PageRenderTime 324ms CodeModel.GetById 141ms app.highlight 17ms RepoModel.GetById 164ms app.codeStats 0ms

/tests/modeltests/model_formsets/models.py

https://code.google.com/p/mango-py/
Python | 193 lines | 131 code | 52 blank | 10 comment | 0 complexity | 759244b0d7824ead9b52f48baf14cc00 MD5 | raw file
  1import datetime
  2from django.db import models
  3
  4class Author(models.Model):
  5    name = models.CharField(max_length=100)
  6
  7    class Meta:
  8        ordering = ('name',)
  9
 10    def __unicode__(self):
 11        return self.name
 12
 13class BetterAuthor(Author):
 14    write_speed = models.IntegerField()
 15
 16class Book(models.Model):
 17    author = models.ForeignKey(Author)
 18    title = models.CharField(max_length=100)
 19
 20    class Meta:
 21        unique_together = (
 22            ('author', 'title'),
 23        )
 24        ordering = ['id']
 25
 26    def __unicode__(self):
 27        return self.title
 28
 29class BookWithCustomPK(models.Model):
 30    my_pk = models.DecimalField(max_digits=5, decimal_places=0, primary_key=True)
 31    author = models.ForeignKey(Author)
 32    title = models.CharField(max_length=100)
 33
 34    def __unicode__(self):
 35        return u'%s: %s' % (self.my_pk, self.title)
 36
 37class Editor(models.Model):
 38    name = models.CharField(max_length=100)
 39
 40class BookWithOptionalAltEditor(models.Model):
 41    author = models.ForeignKey(Author)
 42    # Optional secondary author
 43    alt_editor = models.ForeignKey(Editor, blank=True, null=True)
 44    title = models.CharField(max_length=100)
 45
 46    class Meta:
 47        unique_together = (
 48            ('author', 'title', 'alt_editor'),
 49        )
 50
 51    def __unicode__(self):
 52        return self.title
 53
 54class AlternateBook(Book):
 55    notes = models.CharField(max_length=100)
 56
 57    def __unicode__(self):
 58        return u'%s - %s' % (self.title, self.notes)
 59
 60class AuthorMeeting(models.Model):
 61    name = models.CharField(max_length=100)
 62    authors = models.ManyToManyField(Author)
 63    created = models.DateField(editable=False)
 64
 65    def __unicode__(self):
 66        return self.name
 67
 68class CustomPrimaryKey(models.Model):
 69    my_pk = models.CharField(max_length=10, primary_key=True)
 70    some_field = models.CharField(max_length=100)
 71
 72
 73# models for inheritance tests.
 74
 75class Place(models.Model):
 76    name = models.CharField(max_length=50)
 77    city = models.CharField(max_length=50)
 78
 79    def __unicode__(self):
 80        return self.name
 81
 82class Owner(models.Model):
 83    auto_id = models.AutoField(primary_key=True)
 84    name = models.CharField(max_length=100)
 85    place = models.ForeignKey(Place)
 86
 87    def __unicode__(self):
 88        return "%s at %s" % (self.name, self.place)
 89
 90class Location(models.Model):
 91    place = models.ForeignKey(Place, unique=True)
 92    # this is purely for testing the data doesn't matter here :)
 93    lat = models.CharField(max_length=100)
 94    lon = models.CharField(max_length=100)
 95
 96class OwnerProfile(models.Model):
 97    owner = models.OneToOneField(Owner, primary_key=True)
 98    age = models.PositiveIntegerField()
 99
100    def __unicode__(self):
101        return "%s is %d" % (self.owner.name, self.age)
102
103class Restaurant(Place):
104    serves_pizza = models.BooleanField()
105
106    def __unicode__(self):
107        return self.name
108
109class Product(models.Model):
110    slug = models.SlugField(unique=True)
111
112    def __unicode__(self):
113        return self.slug
114
115class Price(models.Model):
116    price = models.DecimalField(max_digits=10, decimal_places=2)
117    quantity = models.PositiveIntegerField()
118
119    def __unicode__(self):
120        return u"%s for %s" % (self.quantity, self.price)
121
122    class Meta:
123        unique_together = (('price', 'quantity'),)
124
125class MexicanRestaurant(Restaurant):
126    serves_tacos = models.BooleanField()
127
128class ClassyMexicanRestaurant(MexicanRestaurant):
129    restaurant = models.OneToOneField(MexicanRestaurant, parent_link=True, primary_key=True)
130    tacos_are_yummy = models.BooleanField()
131
132# models for testing unique_together validation when a fk is involved and
133# using inlineformset_factory.
134class Repository(models.Model):
135    name = models.CharField(max_length=25)
136
137    def __unicode__(self):
138        return self.name
139
140class Revision(models.Model):
141    repository = models.ForeignKey(Repository)
142    revision = models.CharField(max_length=40)
143
144    class Meta:
145        unique_together = (("repository", "revision"),)
146
147    def __unicode__(self):
148        return u"%s (%s)" % (self.revision, unicode(self.repository))
149
150# models for testing callable defaults (see bug #7975). If you define a model
151# with a callable default value, you cannot rely on the initial value in a
152# form.
153class Person(models.Model):
154    name = models.CharField(max_length=128)
155
156class Membership(models.Model):
157    person = models.ForeignKey(Person)
158    date_joined = models.DateTimeField(default=datetime.datetime.now)
159    karma = models.IntegerField()
160
161# models for testing a null=True fk to a parent
162class Team(models.Model):
163    name = models.CharField(max_length=100)
164
165class Player(models.Model):
166    team = models.ForeignKey(Team, null=True)
167    name = models.CharField(max_length=100)
168
169    def __unicode__(self):
170        return self.name
171
172# Models for testing custom ModelForm save methods in formsets and inline formsets
173class Poet(models.Model):
174    name = models.CharField(max_length=100)
175
176    def __unicode__(self):
177        return self.name
178
179class Poem(models.Model):
180    poet = models.ForeignKey(Poet)
181    name = models.CharField(max_length=100)
182
183    def __unicode__(self):
184        return self.name
185
186class Post(models.Model):
187    title = models.CharField(max_length=50, unique_for_date='posted', blank=True)
188    slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
189    subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
190    posted = models.DateField()
191
192    def __unicode__(self):
193        return self.name