Skip to content

Instantly share code, notes, and snippets.

@zachseifts
Created October 30, 2009 01:43
Show Gist options
  • Save zachseifts/222011 to your computer and use it in GitHub Desktop.
Save zachseifts/222011 to your computer and use it in GitHub Desktop.
class Person(models.Model):
name = models.CharField(_('name'), max_length=128)
parent = models.ForeignKey('self')
# parent, assuming this person is spontaneously created
fred = Person(name='Fred')
# me and my brothers
zach = Person.objects.get_or_create(name='Zach', parent=fred)
trevor = Person.objects.get_or_create(name='Trevor', parent=fred)
austin = Person.objects.get_or_create(name='Austin', parent=fred)
zach.parent_set.all()
# [fred]
austin.parent_set.all()
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment