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 django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Drop the "defaults" kwarg to get_or_create as apparently you're not using it. Also drop the conditional on those set/save statements.
My application is VERY low volume, so I just do this to KISS
(resident, created) = models.Resident.objects.get_or_create(pk=resid, defaults=r)
resident.__dict__.update(r)
resident.save()
--
You received this message because you are subscribed to the Google Groups "Django users" group.