Skip to content

Instantly share code, notes, and snippets.

@joshourisman
Created November 27, 2012 21:47
Show Gist options
  • Save joshourisman/4157318 to your computer and use it in GitHub Desktop.
Save joshourisman/4157318 to your computer and use it in GitHub Desktop.
try:
django_obj = self.django_model.objects.get(my_field=value)
except self.django_model.DoesNotExist:
try:
django_obj = self.django_model.objects.get(**{
'other_field1': other_value1,
'other_field2': other_value2,
'other_field3': other_value3,
'other_field4': other_value4,
})
django_obj.my_field = value
django_obj.save()
except self.django_model.DoesNotExist:
django_obj = self.django_model.objects.create(my_field=value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment