1
from __future__ import unicode_literals
5
from django.db import models
6
from django.utils import six
7
from django.utils.encoding import python_2_unicode_compatible
10
@python_2_unicode_compatible
11
class Author(models.Model):
12
name = models.CharField(max_length=100)
20
class BetterAuthor(Author):
21
write_speed = models.IntegerField()
23
@python_2_unicode_compatible
24
class Book(models.Model):
25
author = models.ForeignKey(Author)
26
title = models.CharField(max_length=100)
37
@python_2_unicode_compatible
38
class BookWithCustomPK(models.Model):
39
my_pk = models.DecimalField(max_digits=5, decimal_places=0, primary_key=True)
40
author = models.ForeignKey(Author)
41
title = models.CharField(max_length=100)
44
return '%s: %s' % (self.my_pk, self.title)
46
class Editor(models.Model):
47
name = models.CharField(max_length=100)
49
@python_2_unicode_compatible
50
class BookWithOptionalAltEditor(models.Model):
51
author = models.ForeignKey(Author)
52
# Optional secondary author
53
alt_editor = models.ForeignKey(Editor, blank=True, null=True)
54
title = models.CharField(max_length=100)
58
('author', 'title', 'alt_editor'),
64
@python_2_unicode_compatible
65
class AlternateBook(Book):
66
notes = models.CharField(max_length=100)
69
return '%s - %s' % (self.title, self.notes)
71
@python_2_unicode_compatible
72
class AuthorMeeting(models.Model):
73
name = models.CharField(max_length=100)
74
authors = models.ManyToManyField(Author)
75
created = models.DateField(editable=False)
80
class CustomPrimaryKey(models.Model):
81
my_pk = models.CharField(max_length=10, primary_key=True)
82
some_field = models.CharField(max_length=100)
85
# models for inheritance tests.
87
@python_2_unicode_compatible
88
class Place(models.Model):
89
name = models.CharField(max_length=50)
90
city = models.CharField(max_length=50)
95
@python_2_unicode_compatible
96
class Owner(models.Model):
97
auto_id = models.AutoField(primary_key=True)
98
name = models.CharField(max_length=100)
99
place = models.ForeignKey(Place)
102
return "%s at %s" % (self.name, self.place)
104
class Location(models.Model):
105
place = models.ForeignKey(Place, unique=True)
106
# this is purely for testing the data doesn't matter here :)
107
lat = models.CharField(max_length=100)
108
lon = models.CharField(max_length=100)
110
@python_2_unicode_compatible
111
class OwnerProfile(models.Model):
112
owner = models.OneToOneField(Owner, primary_key=True)
113
age = models.PositiveIntegerField()
116
return "%s is %d" % (self.owner.name, self.age)
118
@python_2_unicode_compatible
119
class Restaurant(Place):
120
serves_pizza = models.BooleanField()
125
@python_2_unicode_compatible
126
class Product(models.Model):
127
slug = models.SlugField(unique=True)
132
@python_2_unicode_compatible
133
class Price(models.Model):
134
price = models.DecimalField(max_digits=10, decimal_places=2)
135
quantity = models.PositiveIntegerField()
138
return "%s for %s" % (self.quantity, self.price)
141
unique_together = (('price', 'quantity'),)
143
class MexicanRestaurant(Restaurant):
144
serves_tacos = models.BooleanField()
146
class ClassyMexicanRestaurant(MexicanRestaurant):
147
restaurant = models.OneToOneField(MexicanRestaurant, parent_link=True, primary_key=True)
148
tacos_are_yummy = models.BooleanField()
150
# models for testing unique_together validation when a fk is involved and
151
# using inlineformset_factory.
152
@python_2_unicode_compatible
153
class Repository(models.Model):
154
name = models.CharField(max_length=25)
159
@python_2_unicode_compatible
160
class Revision(models.Model):
161
repository = models.ForeignKey(Repository)
162
revision = models.CharField(max_length=40)
165
unique_together = (("repository", "revision"),)
168
return "%s (%s)" % (self.revision, six.text_type(self.repository))
170
# models for testing callable defaults (see bug #7975). If you define a model
171
# with a callable default value, you cannot rely on the initial value in a
173
class Person(models.Model):
174
name = models.CharField(max_length=128)
176
class Membership(models.Model):
177
person = models.ForeignKey(Person)
178
date_joined = models.DateTimeField(default=datetime.datetime.now)
179
karma = models.IntegerField()
181
# models for testing a null=True fk to a parent
182
class Team(models.Model):
183
name = models.CharField(max_length=100)
185
@python_2_unicode_compatible
186
class Player(models.Model):
187
team = models.ForeignKey(Team, null=True)
188
name = models.CharField(max_length=100)
193
# Models for testing custom ModelForm save methods in formsets and inline formsets
194
@python_2_unicode_compatible
195
class Poet(models.Model):
196
name = models.CharField(max_length=100)
201
@python_2_unicode_compatible
202
class Poem(models.Model):
203
poet = models.ForeignKey(Poet)
204
name = models.CharField(max_length=100)
209
@python_2_unicode_compatible
210
class Post(models.Model):
211
title = models.CharField(max_length=50, unique_for_date='posted', blank=True)
212
slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
213
subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
214
posted = models.DateField()