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