开发者

Update single field

开发者 https://www.devze.com 2022-12-24 03:40 出处:网络
I have a Customer class which has a representative field....this field is initially blank but when the user opens up the details page of the chosen customer, they\'ll be given the open of representing

I have a Customer class which has a representative field....this field is initially blank but when the user opens up the details page of the chosen customer, they'll be given the open of representing this customer by clicking on a link. The template layout I was thinking of is this:

<strong>Representative: </strong>
{% if customer.representative %}
   {{ customer.representative }}
{% else %}
   <a href="{% url representCustomer customer.id %}">Represent this customer.</a>
{% endif %}
开发者_开发知识库

All that remains is the view to effect this...this is where I'm stuck.


You need to figure out customer from url and you can get current user from request, then it's merely two lines of view code:

customer = Customer.objects.get(pk=1)
customer.representative = Representative.objects.get(user=request.user)
customer.save()

That said, changing data by GET request is not recommended technique.


Just take the representative that the user belongs to and assign it to the representative field of the relevant customer model, then save.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号