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