PageRenderTime 98ms CodeModel.GetById 89ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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