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