PageRenderTime 48ms CodeModel.GetById 15ms app.highlight 28ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/queries/models.py

https://code.google.com/p/mango-py/
Python | 319 lines | 209 code | 88 blank | 22 comment | 0 complexity | 46de8ae436b89ddc826487211e86d017 MD5 | raw file
  1"""
  2Various complex queries that have been problematic in the past.
  3"""
  4
  5import threading
  6
  7from django.db import models
  8
  9class DumbCategory(models.Model):
 10    pass
 11
 12class NamedCategory(DumbCategory):
 13    name = models.CharField(max_length=10)
 14
 15class Tag(models.Model):
 16    name = models.CharField(max_length=10)
 17    parent = models.ForeignKey('self', blank=True, null=True,
 18            related_name='children')
 19    category = models.ForeignKey(NamedCategory, null=True, default=None)
 20
 21    class Meta:
 22        ordering = ['name']
 23
 24    def __unicode__(self):
 25        return self.name
 26
 27class Note(models.Model):
 28    note = models.CharField(max_length=100)
 29    misc = models.CharField(max_length=10)
 30
 31    class Meta:
 32        ordering = ['note']
 33
 34    def __unicode__(self):
 35        return self.note
 36
 37    def __init__(self, *args, **kwargs):
 38        super(Note, self).__init__(*args, **kwargs)
 39        # Regression for #13227 -- having an attribute that
 40        # is unpickleable doesn't stop you from cloning queries
 41        # that use objects of that type as an argument.
 42        self.lock = threading.Lock()
 43
 44class Annotation(models.Model):
 45    name = models.CharField(max_length=10)
 46    tag = models.ForeignKey(Tag)
 47    notes = models.ManyToManyField(Note)
 48
 49    def __unicode__(self):
 50        return self.name
 51
 52class ExtraInfo(models.Model):
 53    info = models.CharField(max_length=100)
 54    note = models.ForeignKey(Note)
 55
 56    class Meta:
 57        ordering = ['info']
 58
 59    def __unicode__(self):
 60        return self.info
 61
 62class Author(models.Model):
 63    name = models.CharField(max_length=10)
 64    num = models.IntegerField(unique=True)
 65    extra = models.ForeignKey(ExtraInfo)
 66
 67    class Meta:
 68        ordering = ['name']
 69
 70    def __unicode__(self):
 71        return self.name
 72
 73class Item(models.Model):
 74    name = models.CharField(max_length=10)
 75    created = models.DateTimeField()
 76    modified = models.DateTimeField(blank=True, null=True)
 77    tags = models.ManyToManyField(Tag, blank=True, null=True)
 78    creator = models.ForeignKey(Author)
 79    note = models.ForeignKey(Note)
 80
 81    class Meta:
 82        ordering = ['-note', 'name']
 83
 84    def __unicode__(self):
 85        return self.name
 86
 87class Report(models.Model):
 88    name = models.CharField(max_length=10)
 89    creator = models.ForeignKey(Author, to_field='num', null=True)
 90
 91    def __unicode__(self):
 92        return self.name
 93
 94class Ranking(models.Model):
 95    rank = models.IntegerField()
 96    author = models.ForeignKey(Author)
 97
 98    class Meta:
 99        # A complex ordering specification. Should stress the system a bit.
100        ordering = ('author__extra__note', 'author__name', 'rank')
101
102    def __unicode__(self):
103        return '%d: %s' % (self.rank, self.author.name)
104
105class Cover(models.Model):
106    title = models.CharField(max_length=50)
107    item = models.ForeignKey(Item)
108
109    class Meta:
110        ordering = ['item']
111
112    def __unicode__(self):
113        return self.title
114
115class Number(models.Model):
116    num = models.IntegerField()
117
118    def __unicode__(self):
119        return unicode(self.num)
120
121# Symmetrical m2m field with a normal field using the reverse accesor name
122# ("valid").
123class Valid(models.Model):
124    valid = models.CharField(max_length=10)
125    parent = models.ManyToManyField('self')
126
127    class Meta:
128        ordering = ['valid']
129
130# Some funky cross-linked models for testing a couple of infinite recursion
131# cases.
132class X(models.Model):
133    y = models.ForeignKey('Y')
134
135class Y(models.Model):
136    x1 = models.ForeignKey(X, related_name='y1')
137
138# Some models with a cycle in the default ordering. This would be bad if we
139# didn't catch the infinite loop.
140class LoopX(models.Model):
141    y = models.ForeignKey('LoopY')
142
143    class Meta:
144        ordering = ['y']
145
146class LoopY(models.Model):
147    x = models.ForeignKey(LoopX)
148
149    class Meta:
150        ordering = ['x']
151
152class LoopZ(models.Model):
153    z = models.ForeignKey('self')
154
155    class Meta:
156        ordering = ['z']
157
158# A model and custom default manager combination.
159class CustomManager(models.Manager):
160    def get_query_set(self):
161        qs = super(CustomManager, self).get_query_set()
162        return qs.filter(public=True, tag__name='t1')
163
164class ManagedModel(models.Model):
165    data = models.CharField(max_length=10)
166    tag = models.ForeignKey(Tag)
167    public = models.BooleanField(default=True)
168
169    objects = CustomManager()
170    normal_manager = models.Manager()
171
172    def __unicode__(self):
173        return self.data
174
175# An inter-related setup with multiple paths from Child to Detail.
176class Detail(models.Model):
177    data = models.CharField(max_length=10)
178
179class MemberManager(models.Manager):
180    def get_query_set(self):
181        return super(MemberManager, self).get_query_set().select_related("details")
182
183class Member(models.Model):
184    name = models.CharField(max_length=10)
185    details = models.OneToOneField(Detail, primary_key=True)
186
187    objects = MemberManager()
188
189class Child(models.Model):
190    person = models.OneToOneField(Member, primary_key=True)
191    parent = models.ForeignKey(Member, related_name="children")
192
193# Custom primary keys interfered with ordering in the past.
194class CustomPk(models.Model):
195    name = models.CharField(max_length=10, primary_key=True)
196    extra = models.CharField(max_length=10)
197
198    class Meta:
199        ordering = ['name', 'extra']
200
201class Related(models.Model):
202    custom = models.ForeignKey(CustomPk)
203
204# An inter-related setup with a model subclass that has a nullable
205# path to another model, and a return path from that model.
206
207class Celebrity(models.Model):
208    name = models.CharField("Name", max_length=20)
209    greatest_fan = models.ForeignKey("Fan", null=True, unique=True)
210
211class TvChef(Celebrity):
212    pass
213
214class Fan(models.Model):
215    fan_of = models.ForeignKey(Celebrity)
216
217# Multiple foreign keys
218class LeafA(models.Model):
219    data = models.CharField(max_length=10)
220
221    def __unicode__(self):
222        return self.data
223
224class LeafB(models.Model):
225    data = models.CharField(max_length=10)
226
227class Join(models.Model):
228    a = models.ForeignKey(LeafA)
229    b = models.ForeignKey(LeafB)
230
231class ReservedName(models.Model):
232    name = models.CharField(max_length=20)
233    order = models.IntegerField()
234
235    def __unicode__(self):
236        return self.name
237
238# A simpler shared-foreign-key setup that can expose some problems.
239class SharedConnection(models.Model):
240    data = models.CharField(max_length=10)
241
242class PointerA(models.Model):
243    connection = models.ForeignKey(SharedConnection)
244
245class PointerB(models.Model):
246    connection = models.ForeignKey(SharedConnection)
247
248# Multi-layer ordering
249class SingleObject(models.Model):
250    name = models.CharField(max_length=10)
251
252    class Meta:
253        ordering = ['name']
254
255    def __unicode__(self):
256        return self.name
257
258class RelatedObject(models.Model):
259    single = models.ForeignKey(SingleObject)
260
261    class Meta:
262        ordering = ['single']
263
264class Plaything(models.Model):
265    name = models.CharField(max_length=10)
266    others = models.ForeignKey(RelatedObject, null=True)
267
268    class Meta:
269        ordering = ['others']
270
271    def __unicode__(self):
272        return self.name
273
274class Article(models.Model):
275    name = models.CharField(max_length=20)
276    created = models.DateTimeField()
277
278class Food(models.Model):
279    name = models.CharField(max_length=20, unique=True)
280
281    def __unicode__(self):
282        return self.name
283
284class Eaten(models.Model):
285    food = models.ForeignKey(Food, to_field="name")
286    meal = models.CharField(max_length=20)
287
288    def __unicode__(self):
289        return u"%s at %s" % (self.food, self.meal)
290
291class Node(models.Model):
292    num = models.IntegerField(unique=True)
293    parent = models.ForeignKey("self", to_field="num", null=True)
294
295    def __unicode__(self):
296        return u"%s" % self.num
297
298# Bug #12252
299class ObjectA(models.Model):
300    name = models.CharField(max_length=50)
301
302    def __unicode__(self):
303        return self.name
304
305class ObjectB(models.Model):
306    name = models.CharField(max_length=50)
307    objecta = models.ForeignKey(ObjectA)
308    num = models.PositiveSmallIntegerField()
309
310    def __unicode__(self):
311        return self.name
312
313class ObjectC(models.Model):
314    name = models.CharField(max_length=50)
315    objecta = models.ForeignKey(ObjectA)
316    objectb = models.ForeignKey(ObjectB)
317
318    def __unicode__(self):
319       return self.name