On Sun, Mar 15, 2009 at 4:40 PM, Malcolm Tredinnick
<[email protected]> wrote:
> On Sun, 2009-03-15 at 12:12 -0500, Gary Wilson Jr. wrote:
>> Another option would be to allow Model.update() to take the
>> force_update and force_insert parameters that save() does and just
>> pass these through to the save() call.
>
> Aside from the fact that I dislike the ideal as a whole (see below), if
> you're going to have a method called update(), it should do what it says
> on the box. It's not called maybe_create_maybe_update() -- we already
> have that method (it's spelled "save()").

This is the way I lean too, having update() use
save(force_update=True).  We already have Model.__init__ and
Manager.create() for creation.

>> What do you think?
>
> I'm very unconvinced that the idea in the ticket is worth it. It's a
> two-liner if somebody wants to do this in their code, so whilst we *can*
> add this method, adding yet another thing to Model namespace that also
> adds to the documentation and things that need to be tested, etc,
> doesn't seem worth it to me. We already have save(force_update=True) to
> do an update. Adding the extra line to also populate some model fields
> doesn't seem like a great API addition that we can't live without to me.

Sure, the model update() method is a convenience that we can live
without, but I would argue that it rounds out CRUD for models and
makes things a bit more readable:

obj.update(field1=value1, field2=value2)

instead of:

obj.field1 = value1
obj.field2 = value2
obj.save(force_update=True)


IMO, the update_or_create is a bigger win, since otherwise you would
have something like:

defaults = {'field2': value2, 'field3': value3}
obj, created = get_or_create(filed1=value1, defaults=defaults)
if not created:
    obj.update(**defaults)

...which, by the way, without a Model.update() method would look like:

defaults = {'field2': value2, 'field3': value3}
obj, created = get_or_create(filed1=value1, defaults=defaults)
if not created:
    for k, v in defaults.iteritems():
        setattr(obj, k, v)
    obj.save(force_update=True)

...but with update_or_create looks like:

obj, created = update_or_create(filed1=value1, defaults={'field2':
value2, 'field3': value3})


With Model.update() and Manager.update_or_create(), we are saving two
to five lines of code for operations that are fairly common.

Gary

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to