I have a use case where I am retroactively adding save(update_fields=...) 
to an existing code base.  Sometimes the code is structured such that 
'self' of the model is modified in other helper methods of the model, but 
not saved immediately.  It seems like it would be useful to restructure the 
code such that these other methods modify a dict I pass around, and then 
when I am ready to save, I pass that dict to save() -- rather than guessing 
-- and hoping -- that I've listed (and kept up to date!) all of the 
relevant fields in the update_fields list.

It's kinda like:

class Foo(Model):
 
     def _go_do_the_complicated_stuff(self):
         self.a = ...
         self.b = ...
         ...

     def doit(self):
         self._go_do_the_complicated_stuff()
         self.done = True
         self.save(update_fields=[ummmm???])

Where it could be:

class Foo(Model):
 
     def _go_do_the_complicated_stuff(self):
         return dict(a = ...,
                     b = ...,
                     ...)

     def doit(self):
         updates = self._go_do_the_complicated_stuff()
         updates['done'] = True
         self.save(update_fields=updates)

I like this because it is easy to inspect what is getting saved (and should 
be saved) at save time.

Implementation-wise, django would check update_fields for a dict (or just 
use another key) and do something like:

 for attr, value in d.iteritems(): 
    setattr(self, attr, value)
 self.save(update_fields=d.keys())

Now, I know I can just make my own mixin (maybe add a save_dict()) to do 
this, but it seemed like something worth sharing.



Rich

-- 
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+unsubscr...@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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/6f7f4bd1-1f63-4c16-92a8-600392792ff1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to