1. from django.db import models
    
  2. 
    
  3. 
    
  4. class Address(models.Model):
    
  5.     company = models.CharField(max_length=1)
    
  6.     customer_id = models.IntegerField()
    
  7. 
    
  8.     class Meta:
    
  9.         unique_together = [
    
  10.             ("company", "customer_id"),
    
  11.         ]
    
  12. 
    
  13. 
    
  14. class Customer(models.Model):
    
  15.     company = models.CharField(max_length=1)
    
  16.     customer_id = models.IntegerField()
    
  17.     address = models.ForeignObject(
    
  18.         Address,
    
  19.         models.CASCADE,
    
  20.         null=True,
    
  21.         # order mismatches the Contact ForeignObject.
    
  22.         from_fields=["company", "customer_id"],
    
  23.         to_fields=["company", "customer_id"],
    
  24.     )
    
  25. 
    
  26.     class Meta:
    
  27.         unique_together = [
    
  28.             ("company", "customer_id"),
    
  29.         ]
    
  30. 
    
  31. 
    
  32. class Contact(models.Model):
    
  33.     company_code = models.CharField(max_length=1)
    
  34.     customer_code = models.IntegerField()
    
  35.     customer = models.ForeignObject(
    
  36.         Customer,
    
  37.         models.CASCADE,
    
  38.         related_name="contacts",
    
  39.         to_fields=["customer_id", "company"],
    
  40.         from_fields=["customer_code", "company_code"],
    
  41.     )