2013-07-29 17:19:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2011-10-13 18:51:33 +00:00
|
|
|
|
2007-07-20 12:07:58 +00:00
|
|
|
from django.contrib.auth.models import User
|
2011-10-13 18:51:33 +00:00
|
|
|
from django.db import models
|
2012-07-20 12:48:51 +00:00
|
|
|
from django.utils import six
|
2012-08-12 10:32:08 +00:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2007-04-06 02:25:58 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
2007-04-06 02:25:58 +00:00
|
|
|
class Animal(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
name = models.CharField(max_length=150)
|
|
|
|
latin_name = models.CharField(max_length=150)
|
2008-08-24 08:12:13 +00:00
|
|
|
count = models.IntegerField()
|
2009-01-03 04:43:58 +00:00
|
|
|
weight = models.FloatField()
|
2008-11-06 11:19:13 +00:00
|
|
|
|
2009-06-09 13:14:40 +00:00
|
|
|
# use a non-default name for the default manager
|
|
|
|
specimens = models.Manager()
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2009-12-14 12:39:20 +00:00
|
|
|
return self.name
|
2007-04-06 02:25:58 +00:00
|
|
|
|
2008-08-24 08:12:13 +00:00
|
|
|
|
2007-04-17 11:11:26 +00:00
|
|
|
class Plant(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
name = models.CharField(max_length=150)
|
2007-04-17 11:11:26 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
# For testing when upper case letter in app name; regression for #4057
|
|
|
|
db_table = "Fixtures_regress_plant"
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
2007-07-20 12:07:58 +00:00
|
|
|
class Stuff(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
name = models.CharField(max_length=20, null=True)
|
2007-07-20 12:07:58 +00:00
|
|
|
owner = models.ForeignKey(User, null=True)
|
2008-06-30 00:38:14 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2012-07-20 12:48:51 +00:00
|
|
|
return six.text_type(self.name) + ' is owned by ' + six.text_type(self.owner)
|
2007-07-20 12:07:58 +00:00
|
|
|
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2008-02-22 12:50:10 +00:00
|
|
|
class Absolute(models.Model):
|
|
|
|
name = models.CharField(max_length=40)
|
|
|
|
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2008-06-30 00:38:14 +00:00
|
|
|
class Parent(models.Model):
|
|
|
|
name = models.CharField(max_length=10)
|
|
|
|
|
2010-03-18 15:22:15 +00:00
|
|
|
class Meta:
|
|
|
|
ordering = ('id',)
|
|
|
|
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2008-06-30 00:38:14 +00:00
|
|
|
class Child(Parent):
|
|
|
|
data = models.CharField(max_length=10)
|
|
|
|
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2014-05-12 16:46:47 +00:00
|
|
|
# Models to regression test #7572, #20820
|
2008-06-30 12:18:29 +00:00
|
|
|
class Channel(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2008-06-30 12:18:29 +00:00
|
|
|
class Article(models.Model):
|
|
|
|
title = models.CharField(max_length=255)
|
|
|
|
channels = models.ManyToManyField(Channel)
|
2008-11-06 11:19:13 +00:00
|
|
|
|
2008-06-30 12:18:29 +00:00
|
|
|
class Meta:
|
|
|
|
ordering = ('id',)
|
2008-02-22 12:50:10 +00:00
|
|
|
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2013-08-20 13:23:25 +00:00
|
|
|
# Subclass of a model with a ManyToManyField for test_ticket_20820
|
|
|
|
class SpecialArticle(Article):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-05-12 16:46:47 +00:00
|
|
|
# Models to regression test #22421
|
|
|
|
class CommonFeature(Article):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
|
|
|
|
class Feature(CommonFeature):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2009-07-27 14:32:30 +00:00
|
|
|
# Models to regression test #11428
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
2009-07-27 14:32:30 +00:00
|
|
|
class Widget(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
|
2009-12-14 12:39:20 +00:00
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2009-12-14 12:39:20 +00:00
|
|
|
return self.name
|
|
|
|
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2009-07-27 14:32:30 +00:00
|
|
|
class WidgetProxy(Widget):
|
|
|
|
class Meta:
|
|
|
|
proxy = True
|
|
|
|
|
2009-12-14 12:39:20 +00:00
|
|
|
|
2010-09-28 14:52:20 +00:00
|
|
|
# Check for forward references in FKs and M2Ms with natural keys
|
2009-12-14 12:39:20 +00:00
|
|
|
class TestManager(models.Manager):
|
|
|
|
def get_by_natural_key(self, key):
|
|
|
|
return self.get(name=key)
|
|
|
|
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
2009-12-14 12:39:20 +00:00
|
|
|
class Store(models.Model):
|
|
|
|
objects = TestManager()
|
|
|
|
name = models.CharField(max_length=255)
|
2012-06-24 17:54:56 +00:00
|
|
|
main = models.ForeignKey('self', null=True)
|
2009-12-14 12:39:20 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2009-12-14 12:39:20 +00:00
|
|
|
return self.name
|
|
|
|
|
|
|
|
def natural_key(self):
|
|
|
|
return (self.name,)
|
|
|
|
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
2009-12-14 12:39:20 +00:00
|
|
|
class Person(models.Model):
|
|
|
|
objects = TestManager()
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2009-12-14 12:39:20 +00:00
|
|
|
return self.name
|
|
|
|
|
|
|
|
# Person doesn't actually have a dependency on store, but we need to define
|
2011-12-17 17:37:24 +00:00
|
|
|
# one to test the behavior of the dependency resolution algorithm.
|
2009-12-14 12:39:20 +00:00
|
|
|
def natural_key(self):
|
|
|
|
return (self.name,)
|
|
|
|
natural_key.dependencies = ['fixtures_regress.store']
|
|
|
|
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
2009-12-14 12:39:20 +00:00
|
|
|
class Book(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
author = models.ForeignKey(Person)
|
|
|
|
stores = models.ManyToManyField(Store)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2012-06-07 16:08:47 +00:00
|
|
|
return '%s by %s (available at %s)' % (
|
2009-12-14 12:39:20 +00:00
|
|
|
self.name,
|
|
|
|
self.author.name,
|
|
|
|
', '.join(s.name for s in self.stores.all())
|
|
|
|
)
|
|
|
|
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2010-03-18 15:22:15 +00:00
|
|
|
class NKManager(models.Manager):
|
|
|
|
def get_by_natural_key(self, data):
|
|
|
|
return self.get(data=data)
|
|
|
|
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
2010-03-18 15:22:15 +00:00
|
|
|
class NKChild(Parent):
|
|
|
|
data = models.CharField(max_length=10, unique=True)
|
|
|
|
objects = NKManager()
|
|
|
|
|
|
|
|
def natural_key(self):
|
2014-05-12 02:09:12 +00:00
|
|
|
return (self.data,)
|
2010-03-18 15:22:15 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2012-06-07 16:08:47 +00:00
|
|
|
return 'NKChild %s:%s' % (self.name, self.data)
|
2010-03-18 15:22:15 +00:00
|
|
|
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
@python_2_unicode_compatible
|
2010-03-18 15:22:15 +00:00
|
|
|
class RefToNKChild(models.Model):
|
|
|
|
text = models.CharField(max_length=10)
|
|
|
|
nk_fk = models.ForeignKey(NKChild, related_name='ref_fks')
|
|
|
|
nk_m2m = models.ManyToManyField(NKChild, related_name='ref_m2ms')
|
|
|
|
|
2012-08-12 10:32:08 +00:00
|
|
|
def __str__(self):
|
2012-06-07 16:08:47 +00:00
|
|
|
return '%s: Reference to %s [%s]' % (
|
2010-03-18 15:22:15 +00:00
|
|
|
self.text,
|
|
|
|
self.nk_fk,
|
|
|
|
', '.join(str(o) for o in self.nk_m2m.all())
|
|
|
|
)
|
|
|
|
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2009-12-17 21:54:37 +00:00
|
|
|
# ome models with pathological circular dependencies
|
|
|
|
class Circle1(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2009-12-17 21:54:37 +00:00
|
|
|
def natural_key(self):
|
2014-05-12 02:09:12 +00:00
|
|
|
return (self.name,)
|
2009-12-17 21:54:37 +00:00
|
|
|
natural_key.dependencies = ['fixtures_regress.circle2']
|
|
|
|
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2009-12-17 21:54:37 +00:00
|
|
|
class Circle2(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2009-12-17 21:54:37 +00:00
|
|
|
def natural_key(self):
|
2014-05-12 02:09:12 +00:00
|
|
|
return (self.name,)
|
2009-12-17 21:54:37 +00:00
|
|
|
natural_key.dependencies = ['fixtures_regress.circle1']
|
|
|
|
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2009-12-17 21:54:37 +00:00
|
|
|
class Circle3(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2009-12-17 21:54:37 +00:00
|
|
|
def natural_key(self):
|
2014-05-12 02:09:12 +00:00
|
|
|
return (self.name,)
|
2009-12-17 21:54:37 +00:00
|
|
|
natural_key.dependencies = ['fixtures_regress.circle3']
|
|
|
|
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2009-12-17 21:54:37 +00:00
|
|
|
class Circle4(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2009-12-17 21:54:37 +00:00
|
|
|
def natural_key(self):
|
2014-05-12 02:09:12 +00:00
|
|
|
return (self.name,)
|
2009-12-17 21:54:37 +00:00
|
|
|
natural_key.dependencies = ['fixtures_regress.circle5']
|
|
|
|
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2009-12-17 21:54:37 +00:00
|
|
|
class Circle5(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2009-12-17 21:54:37 +00:00
|
|
|
def natural_key(self):
|
2014-05-12 02:09:12 +00:00
|
|
|
return (self.name,)
|
2009-12-17 21:54:37 +00:00
|
|
|
natural_key.dependencies = ['fixtures_regress.circle6']
|
|
|
|
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2009-12-17 21:54:37 +00:00
|
|
|
class Circle6(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2009-12-17 21:54:37 +00:00
|
|
|
def natural_key(self):
|
2014-05-12 02:09:12 +00:00
|
|
|
return (self.name,)
|
2009-12-17 21:54:37 +00:00
|
|
|
natural_key.dependencies = ['fixtures_regress.circle4']
|
|
|
|
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2009-12-17 21:54:37 +00:00
|
|
|
class ExternalDependency(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
2010-09-28 14:52:20 +00:00
|
|
|
|
2009-12-17 21:54:37 +00:00
|
|
|
def natural_key(self):
|
2014-05-12 02:09:12 +00:00
|
|
|
return (self.name,)
|
2009-12-17 21:54:37 +00:00
|
|
|
natural_key.dependencies = ['fixtures_regress.book']
|
|
|
|
|
2010-11-12 01:44:22 +00:00
|
|
|
|
|
|
|
# Model for regression test of #11101
|
|
|
|
class Thingy(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
2013-08-21 00:21:21 +00:00
|
|
|
|
|
|
|
|
2014-12-21 13:24:15 +00:00
|
|
|
class M2MToSelf(models.Model):
|
2014-12-23 18:01:03 +00:00
|
|
|
parent = models.ManyToManyField("self", blank=True)
|
2014-12-21 13:24:15 +00:00
|
|
|
|
|
|
|
|
2013-08-21 00:21:21 +00:00
|
|
|
@python_2_unicode_compatible
|
|
|
|
class BaseNKModel(models.Model):
|
|
|
|
"""
|
|
|
|
Base model with a natural_key and a manager with `get_by_natural_key`
|
|
|
|
"""
|
|
|
|
data = models.CharField(max_length=20, unique=True)
|
|
|
|
objects = NKManager()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.data
|
|
|
|
|
|
|
|
def natural_key(self):
|
|
|
|
return (self.data,)
|
|
|
|
|
|
|
|
|
|
|
|
class M2MSimpleA(BaseNKModel):
|
|
|
|
b_set = models.ManyToManyField("M2MSimpleB")
|
|
|
|
|
|
|
|
|
|
|
|
class M2MSimpleB(BaseNKModel):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class M2MSimpleCircularA(BaseNKModel):
|
|
|
|
b_set = models.ManyToManyField("M2MSimpleCircularB")
|
|
|
|
|
|
|
|
|
|
|
|
class M2MSimpleCircularB(BaseNKModel):
|
|
|
|
a_set = models.ManyToManyField("M2MSimpleCircularA")
|
|
|
|
|
|
|
|
|
|
|
|
class M2MComplexA(BaseNKModel):
|
|
|
|
b_set = models.ManyToManyField("M2MComplexB", through="M2MThroughAB")
|
|
|
|
|
|
|
|
|
|
|
|
class M2MComplexB(BaseNKModel):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class M2MThroughAB(BaseNKModel):
|
|
|
|
a = models.ForeignKey(M2MComplexA)
|
|
|
|
b = models.ForeignKey(M2MComplexB)
|
|
|
|
|
|
|
|
|
|
|
|
class M2MComplexCircular1A(BaseNKModel):
|
|
|
|
b_set = models.ManyToManyField("M2MComplexCircular1B",
|
|
|
|
through="M2MCircular1ThroughAB")
|
|
|
|
|
|
|
|
|
|
|
|
class M2MComplexCircular1B(BaseNKModel):
|
|
|
|
c_set = models.ManyToManyField("M2MComplexCircular1C",
|
|
|
|
through="M2MCircular1ThroughBC")
|
|
|
|
|
|
|
|
|
|
|
|
class M2MComplexCircular1C(BaseNKModel):
|
|
|
|
a_set = models.ManyToManyField("M2MComplexCircular1A",
|
|
|
|
through="M2MCircular1ThroughCA")
|
|
|
|
|
|
|
|
|
|
|
|
class M2MCircular1ThroughAB(BaseNKModel):
|
|
|
|
a = models.ForeignKey(M2MComplexCircular1A)
|
|
|
|
b = models.ForeignKey(M2MComplexCircular1B)
|
|
|
|
|
|
|
|
|
|
|
|
class M2MCircular1ThroughBC(BaseNKModel):
|
|
|
|
b = models.ForeignKey(M2MComplexCircular1B)
|
|
|
|
c = models.ForeignKey(M2MComplexCircular1C)
|
|
|
|
|
|
|
|
|
|
|
|
class M2MCircular1ThroughCA(BaseNKModel):
|
|
|
|
c = models.ForeignKey(M2MComplexCircular1C)
|
|
|
|
a = models.ForeignKey(M2MComplexCircular1A)
|
|
|
|
|
|
|
|
|
|
|
|
class M2MComplexCircular2A(BaseNKModel):
|
|
|
|
b_set = models.ManyToManyField("M2MComplexCircular2B",
|
|
|
|
through="M2MCircular2ThroughAB")
|
|
|
|
|
|
|
|
|
|
|
|
class M2MComplexCircular2B(BaseNKModel):
|
|
|
|
def natural_key(self):
|
|
|
|
return (self.data,)
|
|
|
|
# Fake the dependency for a circularity
|
|
|
|
natural_key.dependencies = ["fixtures_regress.M2MComplexCircular2A"]
|
|
|
|
|
|
|
|
|
|
|
|
class M2MCircular2ThroughAB(BaseNKModel):
|
|
|
|
a = models.ForeignKey(M2MComplexCircular2A)
|
|
|
|
b = models.ForeignKey(M2MComplexCircular2B)
|