On 06/12/2012 05:44 AM, Jerome Baum wrote:
Drop the "defaults" kwarg to get_or_create as apparently you're not
using it. Also drop the conditional on those set/save statements.
Wouldn't this create more db accesses than the original code?
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 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.