Drop the "defaults" kwarg to get_or_create as apparently you're not using 
it. Also drop the conditional on those set/save statements.


On Monday, June 11, 2012 3:44:47 PM UTC+2, ojno 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/-/j3xDyLfgT5wJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to