On Friday, March 2, 2012 7:28:30 AM UTC+2, Carl Meyer wrote:
>
> On 03/01/2012 09:46 PM, Tai Lee wrote:
> > This is easy to achieve in your own code. Create your own BaseModel
> > class that all your models subclass. Instead of using
> > `.filter().update()`, do this:
> > 
> > def update(self, **kwargs):
> >         """
> >         Updates the fields specified in `kwargs` and then call `save()`.
> >         """
> >         if kwargs:
> >                 for k, v in kwargs.iteritems():
> >                         setattr(self, k, v)
> >                 self.save(force_update=True)
> > 
> > This will still fire the `save` signals, and your model instance will
> > have the new values as well as having them saved to the database.
> > 
> > I think it is also easier than assigning values to each field and then
> > calling `.save(only_fields[field1, field2])`, which I don't think is
> > any better than filter().update(). If `save(only_fields=...)` were to
> > only write specified fields to the database, this might cause
> > unexpected side effects with `save` signals?
>
> The "only_fields" kwarg suggestion comes from ticket 4102, and it is
> primarily intended as a performance optimization for large models by
> only sending the values of certain fields to the database at all,
> something that your proposed code does not do.
>
I should have mentioned that ticket. This is one of the tickets where the 
feature isn't hard to implement. Getting the API exactly correct is hard. 
And keeping the feature restricted is even harder...

 - Anssi

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-developers/-/aqdLL7PP1AsJ.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to