2006-05-02 01:31:56 +00:00
|
|
|
"""
|
|
|
|
24. Mutually referential many-to-one relationships
|
|
|
|
|
2008-02-26 21:13:16 +00:00
|
|
|
Strings can be used instead of model literals to set up "lazy" relations.
|
2006-05-02 01:31:56 +00:00
|
|
|
"""
|
|
|
|
|
2011-10-13 12:53:02 +00:00
|
|
|
from django.db import models
|
2006-05-02 01:31:56 +00:00
|
|
|
|
2011-10-13 12:53:02 +00:00
|
|
|
|
|
|
|
class Parent(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
2010-10-12 00:55:05 +00:00
|
|
|
|
2008-02-26 21:13:16 +00:00
|
|
|
# Use a simple string for forward declarations.
|
2011-10-13 12:53:02 +00:00
|
|
|
bestchild = models.ForeignKey("Child", null=True, related_name="favoured_by")
|
2006-05-02 01:31:56 +00:00
|
|
|
|
2011-10-13 12:53:02 +00:00
|
|
|
class Child(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
2010-10-12 00:55:05 +00:00
|
|
|
|
2008-02-26 21:13:16 +00:00
|
|
|
# You can also explicitally specify the related app.
|
2011-10-13 12:53:02 +00:00
|
|
|
parent = models.ForeignKey("mutually_referential.Parent")
|