Skip to content

Instantly share code, notes, and snippets.

@earthling-shruti
Last active December 22, 2015 20:39
Show Gist options
  • Save earthling-shruti/6528300 to your computer and use it in GitHub Desktop.
Save earthling-shruti/6528300 to your computer and use it in GitHub Desktop.
class Model1(models.Model):
field1 = models.CharField()
field2 = models.BooleanField()
obj2 = models.OneToOneField('Model2', null=True, blank=True)
obj3 = models.OneToOneField('Model3', null=True, blank=True)
class Model2(models.Model):
...
@classmethod
def get_model1(cls, some_unique_model2_field):
model2 = Model2.objects.get(some_unique_model2_field=some_unique_model2_field)
model1, created = Model1.get_or_create(model2=model2, defaults={...})
return model1
class Model3(models.Model):
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment