models.py 566 Bytes
Newer Older
1 2 3
"""
24. Mutually referential many-to-one relationships

4
Strings can be used instead of model literals to set up "lazy" relations.
5 6
"""

7
from django.db import models
8

9 10 11

class Parent(models.Model):
    name = models.CharField(max_length=100)
12

13
    # Use a simple string for forward declarations.
14
    bestchild = models.ForeignKey("Child", null=True, related_name="favoured_by")
15

Jason Myers's avatar
Jason Myers committed
16

17 18
class Child(models.Model):
    name = models.CharField(max_length=100)
19

20
    # You can also explicitally specify the related app.
21
    parent = models.ForeignKey("mutually_referential.Parent")