Django 更新单个字段
我有一个Django 更新单个字段,django,Django,我有一个Customer类,该类有一个representative字段……该字段最初为空,但当用户打开所选客户的详细信息页面时,他们将通过单击链接获得表示该客户的权限。我想到的模板布局是: <strong>Representative: </strong> {% if customer.representative %} {{ customer.representative }} {% else %} <a href="{% url representC
Customer
类,该类有一个representative
字段……该字段最初为空,但当用户打开所选客户的详细信息页面时,他们将通过单击链接获得表示该客户的权限。我想到的模板布局是:
<strong>Representative: </strong>
{% if customer.representative %}
{{ customer.representative }}
{% else %}
<a href="{% url representCustomer customer.id %}">Represent this customer.</a>
{% endif %}
代表:
{%if customer.representative%}
{{customer.representation}}
{%else%}
{%endif%}
剩下的就是实现这一点的视图…这就是我被卡住的地方。只需将用户所属的代表分配到相关客户模型的
代表
字段,然后保存。只需将用户所属的代表分配到相关客户模型的代表
字段,然后保存。您需要从url中找出客户,然后可以从请求中获取当前用户,然后只需两行查看代码:
customer = Customer.objects.get(pk=1)
customer.representative = Representative.objects.get(user=request.user)
customer.save()
也就是说,通过GET请求更改数据不是推荐的技术。您需要从url中找出客户,然后您可以从请求中获取当前用户,然后只需两行查看代码:
customer = Customer.objects.get(pk=1)
customer.representative = Representative.objects.get(user=request.user)
customer.save()
也就是说,通过GET请求更改数据不是推荐的技术。如何做到这一点是困扰我的问题……是的,我知道整体机制,但我仍然不知道实际实现……如何做到这一点是困扰我的问题……是的,我知道整体机制,但是实际的实现对我来说还是未知的…啊…谢谢Dmitry。这就像一个符咒…我也会记住提示。啊…谢谢德米特里。这就像一个符咒…我会记住提示也。