Wednesday, August 14, 2013

Re: Does Model.save() write back all the field values?

It's not a bug; that is by design.

However, it's not perfect, so in 1.5 we added update_fields: https://docs.djangoproject.com/en/1.5/ref/models/instances/#specifying-which-fields-to-save

Jacob


On Wed, Aug 14, 2013 at 7:01 AM, Dong Wang <chinuxwang@gmail.com> wrote:
Hi all,

I'm using Django 1.4.2. Please let me start my question by an example:

books = Book.objects.filter(published=False)
for book in books:
book.published=True
book.save() 

I would think this small snippet of code will only update the "published" field, but actually it write all the cached field values back to the database. 

Is it a bug or designed to be this way?

Thanks
-- 
Dong Wang

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment