1. """
    
  2. Mutually referential many-to-one relationships
    
  3. 
    
  4. Strings can be used instead of model literals to set up "lazy" relations.
    
  5. """
    
  6. 
    
  7. from django.db import models
    
  8. 
    
  9. 
    
  10. class Parent(models.Model):
    
  11.     name = models.CharField(max_length=100)
    
  12. 
    
  13.     # Use a simple string for forward declarations.
    
  14.     bestchild = models.ForeignKey(
    
  15.         "Child", models.SET_NULL, null=True, related_name="favored_by"
    
  16.     )
    
  17. 
    
  18. 
    
  19. class Child(models.Model):
    
  20.     name = models.CharField(max_length=100)
    
  21. 
    
  22.     # You can also explicitly specify the related app.
    
  23.     parent = models.ForeignKey("mutually_referential.Parent", models.CASCADE)