2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2014-09-24 13:13:13 +08:00
|
|
|
Mutually referential many-to-one relationships
|
2006-05-02 09:31:56 +08:00
|
|
|
|
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.
|
2014-03-02 22:25:53 +08:00
|
|
|
bestchild = models.ForeignKey("Child", null=True, related_name="favored_by")
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-11-03 05:34:05 +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
|
|
|
|
2014-03-02 22:25:53 +08:00
|
|
|
# You can also explicitly specify the related app.
|
2011-10-13 20:53:02 +08:00
|
|
|
parent = models.ForeignKey("mutually_referential.Parent")
|