Skip to content

Instantly share code, notes, and snippets.

@johnboxall
Created February 2, 2009 16:30
Show Gist options
  • Save johnboxall/56966 to your computer and use it in GitHub Desktop.
Save johnboxall/56966 to your computer and use it in GitHub Desktop.
>>> class Person(models.Model):
... name = models.CharField(max_length=20)
... parent = models.ForeignKey('self', null=True)
>>>
>>> p1 = Person.objects.create(name="Grandpa")
>>> p2 = Person.objects.create(name="Dad", parent=p1)
>>> p3 = Person.objects.create(name="Child", parent=p2)
>>> who_has_a_grandpa = Person.objects.filter(parent__parent__name="Grandpa")
>>> who_has_a_grandpa.name
u'Child'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment