> On Feb 4, 9:10 am, Ashe <matthieu....@gmail.com> wrote:
>
> > Is there a better solution to modify only the specified field on a
> > table than :
> > self.__class__.objects.filter(id=self.id).update(myfield='new awesome
> > value')
> > ?
> > I have been looking for a cleaner solution for a long time without
> > success, while waiting for that (https://code.djangoproject.com/ticket/4102
> > ).
>
> > If it is the best solution, is there any drawback with that solution ?
> > I suppose there is a read on the database because of the
> > objects.filter(). And we lose the possibility to use save() and
> > clean() before update, which I usually enjoy to override.
>
> That is the only available solution currently, apart of raw SQL which
> is an often overlooked alternative.
>
> You could do that update in an overridden .save method. Make it take a
> kwarg 'update_fields=[],' or 'only_myfield=False'. Then, you can just
> use model.save(only_myfield=True). The save method could then handle
> all the necessary dirty details.
>
> BTW even if there is a filter operation, there will be just only one
> query. Actually, your method will be more effective than normal save,
> as without force_update the current implementation of .save() needs to
> first do a select, then update. You can see the queries made by using
> django-debug-toolbar, or just by running this in the shell:
> from django.db import connection
> from django.conf import settings
> settings.DEBUG = True
> self.__class__.objects.filter(id=self.id).update(myfield='new awesome
> value')
> print connection.queries
Thanks for the answer and advice, I will give them a try.
--
Ashe
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment