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
|
|
|
"""
|
|
|
|
|
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
|
2012-08-12 18:32:08 +08:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2007-03-01 21:11:08 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2009-02-28 13:35:22 +08:00
|
|
|
class Category(models.Model):
|
|
|
|
title = models.CharField(max_length=100)
|
|
|
|
description = models.TextField()
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2009-02-28 13:35:22 +08:00
|
|
|
return self.title
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('title',)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
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()
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2007-03-01 21:11:08 +08: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 20:11:04 +08:00
|
|
|
|
2007-03-01 21:11:08 +08: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 20:11:04 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2009-12-14 20:39:20 +08:00
|
|
|
class Blog(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
featured = models.ForeignKey(Article, related_name='fixtures_featured_set')
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2009-12-14 20:39:20 +08:00
|
|
|
class Tag(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
tagged_type = models.ForeignKey(ContentType, related_name="fixtures_tag_set")
|
|
|
|
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
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2009-12-14 20:39:20 +08:00
|
|
|
class Person(models.Model):
|
|
|
|
objects = PersonManager()
|
|
|
|
name = models.CharField(max_length=100)
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2009-12-14 20:39:20 +08:00
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
|
|
|
|
|
|
|
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):
|
|
|
|
return super(SpyManager, self).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
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2009-12-14 20:39:20 +08:00
|
|
|
class Visa(models.Model):
|
|
|
|
person = models.ForeignKey(Person)
|
|
|
|
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
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2009-12-14 20:39:20 +08:00
|
|
|
class Book(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
authors = models.ManyToManyField(Person)
|
|
|
|
|
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
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|