2007-03-01 14:11:08 +01:00
|
|
|
"""
|
2014-09-24 07:13:13 +02:00
|
|
|
Fixtures.
|
2007-03-01 14:11:08 +01: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 14:11:04 +02: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 14:11:08 +01:00
|
|
|
are identified by name, and are stored in either a directory named 'fixtures'
|
2010-12-14 01:37:42 +01:00
|
|
|
in the application directory, or in one of the directories named in the
|
2008-08-12 16:15:38 +02:00
|
|
|
``FIXTURE_DIRS`` setting.
|
2007-03-01 14:11:08 +01:00
|
|
|
"""
|
|
|
|
|
2016-07-16 12:27:25 +02:00
|
|
|
import uuid
|
|
|
|
|
2009-12-14 13:39:20 +01:00
|
|
|
from django.contrib.auth.models import Permission
|
2014-01-22 07:43:33 +01:00
|
|
|
from django.contrib.contenttypes.fields import GenericForeignKey
|
2009-12-14 13:39:20 +01:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2011-10-13 20:04:12 +02:00
|
|
|
from django.db import models
|
2007-03-01 14:11:08 +01:00
|
|
|
|
2009-12-22 16:18:51 +01:00
|
|
|
|
2009-02-28 06:35:22 +01:00
|
|
|
class Category(models.Model):
|
|
|
|
title = models.CharField(max_length=100)
|
|
|
|
description = models.TextField()
|
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2009-02-28 06:35:22 +01:00
|
|
|
return self.title
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('title',)
|
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2007-03-01 14:11:08 +01:00
|
|
|
class Article(models.Model):
|
2007-08-05 07:14:46 +02:00
|
|
|
headline = models.CharField(max_length=100, default='Default headline')
|
2007-03-01 14:11:08 +01:00
|
|
|
pub_date = models.DateTimeField()
|
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2007-03-01 14:11:08 +01:00
|
|
|
return self.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 14:11:04 +02:00
|
|
|
|
2007-03-01 14:11:08 +01:00
|
|
|
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 14:11:04 +02:00
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2009-12-14 13:39:20 +01:00
|
|
|
class Blog(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
2015-07-22 16:43:21 +02:00
|
|
|
featured = models.ForeignKey(Article, models.CASCADE, related_name='fixtures_featured_set')
|
2009-12-14 13:39:20 +01:00
|
|
|
articles = models.ManyToManyField(Article, blank=True,
|
|
|
|
related_name='fixtures_articles_set')
|
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2009-12-14 13:39:20 +01:00
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
|
|
|
class Tag(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
2015-07-22 16:43:21 +02:00
|
|
|
tagged_type = models.ForeignKey(ContentType, models.CASCADE, related_name="fixtures_tag_set")
|
2009-12-14 13:39:20 +01:00
|
|
|
tagged_id = models.PositiveIntegerField(default=0)
|
2014-01-22 07:43:33 +01:00
|
|
|
tagged = GenericForeignKey(ct_field='tagged_type', fk_field='tagged_id')
|
2009-12-14 13:39:20 +01:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2009-12-14 13:39:20 +01:00
|
|
|
return '<%s: %s> tagged "%s"' % (self.tagged.__class__.__name__,
|
|
|
|
self.tagged, self.name)
|
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2009-12-14 13:39:20 +01:00
|
|
|
class PersonManager(models.Manager):
|
|
|
|
def get_by_natural_key(self, name):
|
|
|
|
return self.get(name=name)
|
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2009-12-14 13:39:20 +01:00
|
|
|
class Person(models.Model):
|
|
|
|
objects = PersonManager()
|
|
|
|
name = models.CharField(max_length=100)
|
2013-10-22 12:21:07 +02:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2009-12-14 13:39:20 +01:00
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
|
|
|
|
|
|
|
def natural_key(self):
|
|
|
|
return (self.name,)
|
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2010-08-30 13:58:26 +02:00
|
|
|
class SpyManager(PersonManager):
|
2013-03-08 15:15:23 +01:00
|
|
|
def get_queryset(self):
|
2017-01-21 14:13:44 +01:00
|
|
|
return super().get_queryset().filter(cover_blown=False)
|
2010-08-30 13:58:26 +02:00
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2010-08-30 13:58:26 +02:00
|
|
|
class Spy(Person):
|
|
|
|
objects = SpyManager()
|
|
|
|
cover_blown = models.BooleanField(default=False)
|
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2016-01-27 12:15:38 +01:00
|
|
|
class ProxySpy(Spy):
|
|
|
|
class Meta:
|
|
|
|
proxy = True
|
|
|
|
|
|
|
|
|
2009-12-14 13:39:20 +01:00
|
|
|
class Visa(models.Model):
|
2015-07-22 16:43:21 +02:00
|
|
|
person = models.ForeignKey(Person, models.CASCADE)
|
2009-12-14 13:39:20 +01:00
|
|
|
permissions = models.ManyToManyField(Permission, blank=True)
|
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2009-12-14 13:39:20 +01:00
|
|
|
return '%s %s' % (self.person.name,
|
|
|
|
', '.join(p.name for p in self.permissions.all()))
|
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2009-12-14 13:39:20 +01:00
|
|
|
class Book(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
authors = models.ManyToManyField(Person)
|
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2011-11-27 14:00:56 +01: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 13:39:20 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
2016-07-16 12:27:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
class PrimaryKeyUUIDModel(models.Model):
|
|
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
|