hmm, have you considered creating a custom Model Manager with a custom
method for this purpose? I don't really have any ideas at the moment, sorry!

On Mon, Jun 11, 2012 at 9:44 AM, ojno <[email protected]> wrote:

> Hi all,
>
> In my app, which involves doing background tasks and possibly rerunning
> them a number of times, I find myself using this pattern a lot:
>
> instance, created = Model.objects.get_or_create(key1=key1, key2=key2,
>>                                                 defaults={"field1":field1,
>> "field2":field2})
>> if not created:
>>     instance.field1 = field1
>>     instance.field2 = field2
>>     instance.save()
>
>
> This is unsatisfyingly WET. Does anyone know a better way to do this that
> still preserves all the integrity and lack of race conditions get_or_create
> gives you?
>
> Thanks,
> ojno
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/6f0-bPejkBkJ.
> 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-users?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" 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-users?hl=en.

Reply via email to