~ubuntu-branches/debian/sid/python-django/sid

« back to all changes in this revision

Viewing changes to tests/modeltests/model_formsets/models.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone
  • Date: 2013-11-07 15:33:49 UTC
  • mfrom: (1.3.12)
  • Revision ID: package-import@ubuntu.com-20131107153349-e31sc149l2szs3jb
Tags: 1.6-1
* New upstream version. Closes: #557474, #724637.
* python-django now also suggests the installation of ipython,
  bpython, python-django-doc, and libgdal1.
  Closes: #636511, #686333, #704203
* Set package maintainer to Debian Python Modules Team.
* Bump standards version to 3.9.5, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from __future__ import unicode_literals
2
 
 
3
 
import datetime
4
 
 
5
 
from django.db import models
6
 
from django.utils import six
7
 
from django.utils.encoding import python_2_unicode_compatible
8
 
 
9
 
 
10
 
@python_2_unicode_compatible
11
 
class Author(models.Model):
12
 
    name = models.CharField(max_length=100)
13
 
 
14
 
    class Meta:
15
 
        ordering = ('name',)
16
 
 
17
 
    def __str__(self):
18
 
        return self.name
19
 
 
20
 
class BetterAuthor(Author):
21
 
    write_speed = models.IntegerField()
22
 
 
23
 
@python_2_unicode_compatible
24
 
class Book(models.Model):
25
 
    author = models.ForeignKey(Author)
26
 
    title = models.CharField(max_length=100)
27
 
 
28
 
    class Meta:
29
 
        unique_together = (
30
 
            ('author', 'title'),
31
 
        )
32
 
        ordering = ['id']
33
 
 
34
 
    def __str__(self):
35
 
        return self.title
36
 
 
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)
42
 
 
43
 
    def __str__(self):
44
 
        return '%s: %s' % (self.my_pk, self.title)
45
 
 
46
 
class Editor(models.Model):
47
 
    name = models.CharField(max_length=100)
48
 
 
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)
55
 
 
56
 
    class Meta:
57
 
        unique_together = (
58
 
            ('author', 'title', 'alt_editor'),
59
 
        )
60
 
 
61
 
    def __str__(self):
62
 
        return self.title
63
 
 
64
 
@python_2_unicode_compatible
65
 
class AlternateBook(Book):
66
 
    notes = models.CharField(max_length=100)
67
 
 
68
 
    def __str__(self):
69
 
        return '%s - %s' % (self.title, self.notes)
70
 
 
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)
76
 
 
77
 
    def __str__(self):
78
 
        return self.name
79
 
 
80
 
class CustomPrimaryKey(models.Model):
81
 
    my_pk = models.CharField(max_length=10, primary_key=True)
82
 
    some_field = models.CharField(max_length=100)
83
 
 
84
 
 
85
 
# models for inheritance tests.
86
 
 
87
 
@python_2_unicode_compatible
88
 
class Place(models.Model):
89
 
    name = models.CharField(max_length=50)
90
 
    city = models.CharField(max_length=50)
91
 
 
92
 
    def __str__(self):
93
 
        return self.name
94
 
 
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)
100
 
 
101
 
    def __str__(self):
102
 
        return "%s at %s" % (self.name, self.place)
103
 
 
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)
109
 
 
110
 
@python_2_unicode_compatible
111
 
class OwnerProfile(models.Model):
112
 
    owner = models.OneToOneField(Owner, primary_key=True)
113
 
    age = models.PositiveIntegerField()
114
 
 
115
 
    def __str__(self):
116
 
        return "%s is %d" % (self.owner.name, self.age)
117
 
 
118
 
@python_2_unicode_compatible
119
 
class Restaurant(Place):
120
 
    serves_pizza = models.BooleanField()
121
 
 
122
 
    def __str__(self):
123
 
        return self.name
124
 
 
125
 
@python_2_unicode_compatible
126
 
class Product(models.Model):
127
 
    slug = models.SlugField(unique=True)
128
 
 
129
 
    def __str__(self):
130
 
        return self.slug
131
 
 
132
 
@python_2_unicode_compatible
133
 
class Price(models.Model):
134
 
    price = models.DecimalField(max_digits=10, decimal_places=2)
135
 
    quantity = models.PositiveIntegerField()
136
 
 
137
 
    def __str__(self):
138
 
        return "%s for %s" % (self.quantity, self.price)
139
 
 
140
 
    class Meta:
141
 
        unique_together = (('price', 'quantity'),)
142
 
 
143
 
class MexicanRestaurant(Restaurant):
144
 
    serves_tacos = models.BooleanField()
145
 
 
146
 
class ClassyMexicanRestaurant(MexicanRestaurant):
147
 
    restaurant = models.OneToOneField(MexicanRestaurant, parent_link=True, primary_key=True)
148
 
    tacos_are_yummy = models.BooleanField()
149
 
 
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)
155
 
 
156
 
    def __str__(self):
157
 
        return self.name
158
 
 
159
 
@python_2_unicode_compatible
160
 
class Revision(models.Model):
161
 
    repository = models.ForeignKey(Repository)
162
 
    revision = models.CharField(max_length=40)
163
 
 
164
 
    class Meta:
165
 
        unique_together = (("repository", "revision"),)
166
 
 
167
 
    def __str__(self):
168
 
        return "%s (%s)" % (self.revision, six.text_type(self.repository))
169
 
 
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
172
 
# form.
173
 
class Person(models.Model):
174
 
    name = models.CharField(max_length=128)
175
 
 
176
 
class Membership(models.Model):
177
 
    person = models.ForeignKey(Person)
178
 
    date_joined = models.DateTimeField(default=datetime.datetime.now)
179
 
    karma = models.IntegerField()
180
 
 
181
 
# models for testing a null=True fk to a parent
182
 
class Team(models.Model):
183
 
    name = models.CharField(max_length=100)
184
 
 
185
 
@python_2_unicode_compatible
186
 
class Player(models.Model):
187
 
    team = models.ForeignKey(Team, null=True)
188
 
    name = models.CharField(max_length=100)
189
 
 
190
 
    def __str__(self):
191
 
        return self.name
192
 
 
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)
197
 
 
198
 
    def __str__(self):
199
 
        return self.name
200
 
 
201
 
@python_2_unicode_compatible
202
 
class Poem(models.Model):
203
 
    poet = models.ForeignKey(Poet)
204
 
    name = models.CharField(max_length=100)
205
 
 
206
 
    def __str__(self):
207
 
        return self.name
208
 
 
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()
215
 
 
216
 
    def __str__(self):
217
 
        return self.name