PageRenderTime 27ms CodeModel.GetById 1ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/serializers_regress/models.py

https://code.google.com/p/mango-py/
Python | 260 lines | 139 code | 77 blank | 44 comment | 0 complexity | e93ff13e7124fa64b9824d6177f9fb0a MD5 | raw file
  1"""
  2A test spanning all the capabilities of all the serializers.
  3
  4This class sets up a model for each model field type
  5(except for image types, because of the PIL dependency).
  6"""
  7
  8from django.db import models
  9from django.contrib.contenttypes import generic
 10from django.contrib.contenttypes.models import ContentType
 11from django.contrib.localflavor.us.models import USStateField, PhoneNumberField
 12
 13# The following classes are for testing basic data
 14# marshalling, including NULL values, where allowed.
 15
 16class BooleanData(models.Model):
 17    data = models.BooleanField()
 18
 19class CharData(models.Model):
 20    data = models.CharField(max_length=30, null=True)
 21
 22class DateData(models.Model):
 23    data = models.DateField(null=True)
 24
 25class DateTimeData(models.Model):
 26    data = models.DateTimeField(null=True)
 27
 28class DecimalData(models.Model):
 29    data = models.DecimalField(null=True, decimal_places=3, max_digits=5)
 30
 31class EmailData(models.Model):
 32    data = models.EmailField(null=True)
 33
 34class FileData(models.Model):
 35    data = models.FileField(null=True, upload_to='/foo/bar')
 36
 37class FilePathData(models.Model):
 38    data = models.FilePathField(null=True)
 39
 40class FloatData(models.Model):
 41    data = models.FloatField(null=True)
 42
 43class IntegerData(models.Model):
 44    data = models.IntegerField(null=True)
 45
 46class BigIntegerData(models.Model):
 47    data = models.BigIntegerField(null=True)
 48
 49# class ImageData(models.Model):
 50#    data = models.ImageField(null=True)
 51
 52class IPAddressData(models.Model):
 53    data = models.IPAddressField(null=True)
 54
 55class NullBooleanData(models.Model):
 56    data = models.NullBooleanField(null=True)
 57
 58class PhoneData(models.Model):
 59    data = PhoneNumberField(null=True)
 60
 61class PositiveIntegerData(models.Model):
 62    data = models.PositiveIntegerField(null=True)
 63
 64class PositiveSmallIntegerData(models.Model):
 65    data = models.PositiveSmallIntegerField(null=True)
 66
 67class SlugData(models.Model):
 68    data = models.SlugField(null=True)
 69
 70class SmallData(models.Model):
 71    data = models.SmallIntegerField(null=True)
 72
 73class TextData(models.Model):
 74    data = models.TextField(null=True)
 75
 76class TimeData(models.Model):
 77    data = models.TimeField(null=True)
 78
 79class USStateData(models.Model):
 80    data = USStateField(null=True)
 81
 82class Tag(models.Model):
 83    """A tag on an item."""
 84    data = models.SlugField()
 85    content_type = models.ForeignKey(ContentType)
 86    object_id = models.PositiveIntegerField()
 87
 88    content_object = generic.GenericForeignKey()
 89
 90    class Meta:
 91        ordering = ["data"]
 92
 93class GenericData(models.Model):
 94    data = models.CharField(max_length=30)
 95
 96    tags = generic.GenericRelation(Tag)
 97
 98# The following test classes are all for validation
 99# of related objects; in particular, forward, backward,
100# and self references.
101
102class Anchor(models.Model):
103    """This is a model that can be used as
104    something for other models to point at"""
105
106    data = models.CharField(max_length=30)
107
108    class Meta:
109        ordering = ('id',)
110
111class UniqueAnchor(models.Model):
112    """This is a model that can be used as
113    something for other models to point at"""
114
115    data = models.CharField(unique=True, max_length=30)
116
117class FKData(models.Model):
118    data = models.ForeignKey(Anchor, null=True)
119
120class M2MData(models.Model):
121    data = models.ManyToManyField(Anchor, null=True)
122
123class O2OData(models.Model):
124    # One to one field can't be null here, since it is a PK.
125    data = models.OneToOneField(Anchor, primary_key=True)
126
127class FKSelfData(models.Model):
128    data = models.ForeignKey('self', null=True)
129
130class M2MSelfData(models.Model):
131    data = models.ManyToManyField('self', null=True, symmetrical=False)
132
133class FKDataToField(models.Model):
134    data = models.ForeignKey(UniqueAnchor, null=True, to_field='data')
135
136class FKDataToO2O(models.Model):
137    data = models.ForeignKey(O2OData, null=True)
138
139class M2MIntermediateData(models.Model):
140    data = models.ManyToManyField(Anchor, null=True, through='Intermediate')
141
142class Intermediate(models.Model):
143    left = models.ForeignKey(M2MIntermediateData)
144    right = models.ForeignKey(Anchor)
145    extra = models.CharField(max_length=30, blank=True, default="doesn't matter")
146
147# The following test classes are for validating the
148# deserialization of objects that use a user-defined
149# field as the primary key.
150# Some of these data types have been commented out
151# because they can't be used as a primary key on one
152# or all database backends.
153
154class BooleanPKData(models.Model):
155    data = models.BooleanField(primary_key=True)
156
157class CharPKData(models.Model):
158    data = models.CharField(max_length=30, primary_key=True)
159
160# class DatePKData(models.Model):
161#    data = models.DateField(primary_key=True)
162
163# class DateTimePKData(models.Model):
164#    data = models.DateTimeField(primary_key=True)
165
166class DecimalPKData(models.Model):
167    data = models.DecimalField(primary_key=True, decimal_places=3, max_digits=5)
168
169class EmailPKData(models.Model):
170    data = models.EmailField(primary_key=True)
171
172# class FilePKData(models.Model):
173#    data = models.FileField(primary_key=True, upload_to='/foo/bar')
174
175class FilePathPKData(models.Model):
176    data = models.FilePathField(primary_key=True)
177
178class FloatPKData(models.Model):
179    data = models.FloatField(primary_key=True)
180
181class IntegerPKData(models.Model):
182    data = models.IntegerField(primary_key=True)
183
184# class ImagePKData(models.Model):
185#    data = models.ImageField(primary_key=True)
186
187class IPAddressPKData(models.Model):
188    data = models.IPAddressField(primary_key=True)
189
190# This is just a Boolean field with null=True, and we can't test a PK value of NULL.
191# class NullBooleanPKData(models.Model):
192#     data = models.NullBooleanField(primary_key=True)
193
194class PhonePKData(models.Model):
195    data = PhoneNumberField(primary_key=True)
196
197class PositiveIntegerPKData(models.Model):
198    data = models.PositiveIntegerField(primary_key=True)
199
200class PositiveSmallIntegerPKData(models.Model):
201    data = models.PositiveSmallIntegerField(primary_key=True)
202
203class SlugPKData(models.Model):
204    data = models.SlugField(primary_key=True)
205
206class SmallPKData(models.Model):
207    data = models.SmallIntegerField(primary_key=True)
208
209# class TextPKData(models.Model):
210#     data = models.TextField(primary_key=True)
211
212# class TimePKData(models.Model):
213#    data = models.TimeField(primary_key=True)
214
215class USStatePKData(models.Model):
216    data = USStateField(primary_key=True)
217
218class ComplexModel(models.Model):
219    field1 = models.CharField(max_length=10)
220    field2 = models.CharField(max_length=10)
221    field3 = models.CharField(max_length=10)
222
223# Tests for handling fields with pre_save functions, or
224# models with save functions that modify data
225class AutoNowDateTimeData(models.Model):
226    data = models.DateTimeField(null=True, auto_now=True)
227
228class ModifyingSaveData(models.Model):
229    data = models.IntegerField(null=True)
230
231    def save(self):
232        "A save method that modifies the data in the object"
233        self.data = 666
234        super(ModifyingSaveData, self).save(raw)
235
236# Tests for serialization of models using inheritance.
237# Regression for #7202, #7350
238class AbstractBaseModel(models.Model):
239    parent_data = models.IntegerField()
240    class Meta:
241        abstract = True
242
243class InheritAbstractModel(AbstractBaseModel):
244    child_data = models.IntegerField()
245
246class BaseModel(models.Model):
247    parent_data = models.IntegerField()
248
249class InheritBaseModel(BaseModel):
250    child_data = models.IntegerField()
251
252class ExplicitInheritBaseModel(BaseModel):
253    parent = models.OneToOneField(BaseModel)
254    child_data = models.IntegerField()
255
256class LengthModel(models.Model):
257    data = models.IntegerField()
258
259    def __len__(self):
260        return self.data