Wednesday, August 14, 2013

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

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iQIcBAEBAgAGBQJSC6vbAAoJEJ9WpvTpw+Eqx6cP/iaY7EDxtIMTv3hPM25ucDwI
0qSqDOAPn6HiYJ6ft2qEZt31xWk0lMF/zaQjjxwJnQ1YoKTLMwrCnhGvbd8VKKby
FrMwm4h0El/sftiBqduS6FsWbA0N45kOTOaXDpnx0XMqOAK38u0lbhunHk0KfwYy
mD3UKPa9B8pdYAMgVhnO3OP64eN3C1T3tOOxNu2Shjxgrdjtm3YG5h8JbC+RaYNn
t8Ad+M/fgp0JDVjjsT1Wxv8Gbju9J8ECXMkpPd5nybLgRULCg6WTXXUFkMCvATc8
gvzkX9OAUG68KvPWLETi1J/Oa0rLBbojS2h36T5jU5c6QPwj1ESRumytdKz45572
rcMdQVkb3NcVzFCrRKVTCKdGEuTJqU/s7DqD81rzkE6fJQqBPI8Uj+7WdsxyrTZe
HPfsD/LdhLa7Lk4NeN0omPwTKWf6TR2CPtji/rwakA120paXYKjUfD4s4JKID+Ol
79FQrI2tpz3NCRhtk+h376razzxBP7wcqTDsmTILYOXCrTx3HIiKEg38B2Ygdh+w
fjAOsLI0Ob6SN8rWzR9F6FhBPS2mG8fz8QtzrlHKHRMJQ6/cnTCm84TbL0zyyp5o
DDc3L7QEQ7xcsuK/TQOB04A0AT0BW02P27Xm7lhZK9SFEMi+WGDgVcuZXdvwIl5o
jht2XgzxP+G3H4/8gJ0+
=vX8K
-----END PGP SIGNATURE-----
Hi,
I apologize for possible off-topic, but since Jacob already answered
your question, please allow me to add another hint:

Book.objects.filter(published=False).update(published=True)

would do the same in single query, updating "published" field only (even in <1.5).


Cheers,
Tom


Dne Wed, 14 Aug 2013 10:17:04 -0500
Jacob Kaplan-Moss <jacob@jacobian.org> napsal(a):

> 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.
> >
>

No comments:

Post a Comment