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