customers.py 1012 B

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