2007-03-01 21:11:08 +08:00
|
|
|
"""
|
2014-09-24 13:13:13 +08:00
|
|
|
Fixtures.
|
2007-03-01 21:11:08 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
Fixtures are a way of loading data into the database in bulk. Fixure data
|
|
|
|
can be stored in any serializable format (including JSON and XML). Fixtures
|
2007-03-01 21:11:08 +08:00
|
|
|
are identified by name, and are stored in either a directory named 'fixtures'
|
2010-12-14 08:37:42 +08:00
|
|
|
in the application directory, or in one of the directories named in the
|
2008-08-12 22:15:38 +08:00
|
|
|
``FIXTURE_DIRS`` setting.
|
2007-03-01 21:11:08 +08:00
|
|
|
"""
|
|
|
|
|
2016-07-16 18:27:25 +08:00
|
|
|
import uuid
|
|
|
|
|
2009-12-14 20:39:20 +08:00
|
|
|
from django.contrib.auth.models import Permission
|
2014-01-22 14:43:33 +08:00
|
|
|
from django.contrib.contenttypes.fields import GenericForeignKey
|
2009-12-14 20:39:20 +08:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2011-10-14 02:04:12 +08:00
|
|
|
from django.db import models
|
2007-03-01 21:11:08 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2009-02-28 13:35:22 +08:00
|
|
|
class Category(models.Model):
|
|
|
|
title = models.CharField(max_length=100)
|
|
|
|
description = models.TextField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ("title",)
|
|
|
|
|
2018-12-28 08:34:14 +08:00
|
|
|
def __str__(self):
|
|
|
|
return self.title
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2007-03-01 21:11:08 +08:00
|
|
|
class Article(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
headline = models.CharField(max_length=100, default="Default headline")
|
2007-03-01 21:11:08 +08:00
|
|
|
pub_date = models.DateTimeField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ("-pub_date", "headline")
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2018-12-28 08:34:14 +08:00
|
|
|
def __str__(self):
|
|
|
|
return self.headline
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2009-12-14 20:39:20 +08:00
|
|
|
class Blog(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
2015-07-22 22:43:21 +08:00
|
|
|
featured = models.ForeignKey(
|
|
|
|
Article, models.CASCADE, related_name="fixtures_featured_set"
|
|
|
|
)
|
2009-12-14 20:39:20 +08:00
|
|
|
articles = models.ManyToManyField(
|
|
|
|
Article, blank=True, related_name="fixtures_articles_set"
|
|
|
|
)
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2009-12-14 20:39:20 +08:00
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
|
|
|
class Tag(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
2015-07-22 22:43:21 +08:00
|
|
|
tagged_type = models.ForeignKey(
|
|
|
|
ContentType, models.CASCADE, related_name="fixtures_tag_set"
|
|
|
|
)
|
2009-12-14 20:39:20 +08:00
|
|
|
tagged_id = models.PositiveIntegerField(default=0)
|
2014-01-22 14:43:33 +08:00
|
|
|
tagged = GenericForeignKey(ct_field="tagged_type", fk_field="tagged_id")
|
2009-12-14 20:39:20 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2009-12-14 20:39:20 +08:00
|
|
|
return '<%s: %s> tagged "%s"' % (
|
|
|
|
self.tagged.__class__.__name__,
|
|
|
|
self.tagged,
|
|
|
|
self.name,
|
|
|
|
)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2009-12-14 20:39:20 +08:00
|
|
|
class PersonManager(models.Manager):
|
|
|
|
def get_by_natural_key(self, name):
|
|
|
|
return self.get(name=name)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2009-12-14 20:39:20 +08:00
|
|
|
class Person(models.Model):
|
|
|
|
objects = PersonManager()
|
2020-04-07 16:54:19 +08:00
|
|
|
name = models.CharField(max_length=100, unique=True)
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2009-12-14 20:39:20 +08:00
|
|
|
class Meta:
|
|
|
|
ordering = ("name",)
|
|
|
|
|
2018-12-28 08:34:14 +08:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2009-12-14 20:39:20 +08:00
|
|
|
def natural_key(self):
|
|
|
|
return (self.name,)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2010-08-30 19:58:26 +08:00
|
|
|
class SpyManager(PersonManager):
|
2013-03-08 22:15:23 +08:00
|
|
|
def get_queryset(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().get_queryset().filter(cover_blown=False)
|
2010-08-30 19:58:26 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2010-08-30 19:58:26 +08:00
|
|
|
class Spy(Person):
|
|
|
|
objects = SpyManager()
|
|
|
|
cover_blown = models.BooleanField(default=False)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2016-01-27 19:15:38 +08:00
|
|
|
class ProxySpy(Spy):
|
|
|
|
class Meta:
|
|
|
|
proxy = True
|
|
|
|
|
|
|
|
|
2009-12-14 20:39:20 +08:00
|
|
|
class Visa(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
person = models.ForeignKey(Person, models.CASCADE)
|
2009-12-14 20:39:20 +08:00
|
|
|
permissions = models.ManyToManyField(Permission, blank=True)
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2009-12-14 20:39:20 +08:00
|
|
|
return "%s %s" % (
|
|
|
|
self.person.name,
|
|
|
|
", ".join(p.name for p in self.permissions.all()),
|
|
|
|
)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2009-12-14 20:39:20 +08:00
|
|
|
class Book(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
authors = models.ManyToManyField(Person)
|
|
|
|
|
2018-12-28 08:34:14 +08:00
|
|
|
class Meta:
|
|
|
|
ordering = ("name",)
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2011-11-27 21:00:56 +08:00
|
|
|
authors = " and ".join(a.name for a in self.authors.all())
|
|
|
|
return "%s by %s" % (self.name, authors) if authors else self.name
|
2009-12-14 20:39:20 +08:00
|
|
|
|
2016-07-16 18:27:25 +08:00
|
|
|
|
|
|
|
class PrimaryKeyUUIDModel(models.Model):
|
|
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
|
2018-07-14 05:54:47 +08:00
|
|
|
|
|
|
|
|
2019-12-02 07:50:59 +08:00
|
|
|
class NaturalKeyManager(models.Manager):
|
|
|
|
def get_by_natural_key(self, key):
|
|
|
|
return self.get(key=key)
|
|
|
|
|
|
|
|
|
2018-07-14 05:54:47 +08:00
|
|
|
class NaturalKeyThing(models.Model):
|
2020-04-07 16:54:19 +08:00
|
|
|
key = models.CharField(max_length=100, unique=True)
|
2018-07-14 05:54:47 +08:00
|
|
|
other_thing = models.ForeignKey(
|
|
|
|
"NaturalKeyThing", on_delete=models.CASCADE, null=True
|
|
|
|
)
|
|
|
|
other_things = models.ManyToManyField(
|
|
|
|
"NaturalKeyThing", related_name="thing_m2m_set"
|
|
|
|
)
|
|
|
|
|
2019-12-02 07:50:59 +08:00
|
|
|
objects = NaturalKeyManager()
|
2018-07-14 05:54:47 +08:00
|
|
|
|
|
|
|
def natural_key(self):
|
|
|
|
return (self.key,)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.key
|
2020-04-07 17:28:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
class CircularA(models.Model):
|
|
|
|
key = models.CharField(max_length=3, unique=True)
|
|
|
|
obj = models.ForeignKey("CircularB", models.SET_NULL, null=True)
|
|
|
|
|
2019-12-02 07:50:59 +08:00
|
|
|
objects = NaturalKeyManager()
|
|
|
|
|
|
|
|
def natural_key(self):
|
|
|
|
return (self.key,)
|
|
|
|
|
2020-04-07 17:28:25 +08:00
|
|
|
|
|
|
|
class CircularB(models.Model):
|
|
|
|
key = models.CharField(max_length=3, unique=True)
|
|
|
|
obj = models.ForeignKey("CircularA", models.SET_NULL, null=True)
|
2019-12-02 07:50:59 +08:00
|
|
|
|
|
|
|
objects = NaturalKeyManager()
|
|
|
|
|
|
|
|
def natural_key(self):
|
|
|
|
return (self.key,)
|