Задать вопрос

Тел: +7 965 3737 888

510

Просмотров

0

Ответов

Another means of updating a subset of a model's fields

<p>Based on the UPDATE query section of Model.save(), this is another means of limiting the fields which are used in an UPDATE statement and bypassing the check for object existence which is made when you use Model.save().</p>
<p>Just make whatever changes you want to your model instance and call update, passing your instance and the names of any fields to be updated.</p>
<p>Usage example:</p>
import datetime

from forum.models import Topic
from forum.utils.models import update

topic = Topic.objects.get(pk=1)
topic.post_count += 1
topic.last_post_at = datetime.datetime.now()
update(topic, 'post_count', 'last_post_at')

<p>(Originally intended as a comment on <a href="https://djangosnippets.org/snippets/479/">Snippet 479</a>, but comments aren't working for me)</p>

Вопрос полезен? Да0/Нет0
file_4845.py(760.0б)
None