/tests/modeltests/one_to_one/models.py

https://code.google.com/p/mango-py/ · Python · 47 lines · 29 code · 11 blank · 7 comment · 0 complexity · 01feb3527b50578dd02d575908790b51 MD5 · raw file

  1. """
  2. 10. One-to-one relationships
  3. To define a one-to-one relationship, use ``OneToOneField()``.
  4. In this example, a ``Place`` optionally can be a ``Restaurant``.
  5. """
  6. from django.db import models, transaction, IntegrityError
  7. class Place(models.Model):
  8. name = models.CharField(max_length=50)
  9. address = models.CharField(max_length=80)
  10. def __unicode__(self):
  11. return u"%s the place" % self.name
  12. class Restaurant(models.Model):
  13. place = models.OneToOneField(Place, primary_key=True)
  14. serves_hot_dogs = models.BooleanField()
  15. serves_pizza = models.BooleanField()
  16. def __unicode__(self):
  17. return u"%s the restaurant" % self.place.name
  18. class Waiter(models.Model):
  19. restaurant = models.ForeignKey(Restaurant)
  20. name = models.CharField(max_length=50)
  21. def __unicode__(self):
  22. return u"%s the waiter at %s" % (self.name, self.restaurant)
  23. class ManualPrimaryKey(models.Model):
  24. primary_key = models.CharField(max_length=10, primary_key=True)
  25. name = models.CharField(max_length = 50)
  26. class RelatedModel(models.Model):
  27. link = models.OneToOneField(ManualPrimaryKey)
  28. name = models.CharField(max_length = 50)
  29. class MultiModel(models.Model):
  30. link1 = models.OneToOneField(Place)
  31. link2 = models.OneToOneField(ManualPrimaryKey)
  32. name = models.CharField(max_length=50)
  33. def __unicode__(self):
  34. return u"Multimodel %s" % self.name