models.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """
  2. Fixtures.
  3. Fixtures are a way of loading data into the database in bulk. Fixure data
  4. can be stored in any serializable format (including JSON and XML). Fixtures
  5. are identified by name, and are stored in either a directory named 'fixtures'
  6. in the application directory, or in one of the directories named in the
  7. ``FIXTURE_DIRS`` setting.
  8. """
  9. from django.contrib.auth.models import Permission
  10. from django.contrib.contenttypes.fields import GenericForeignKey
  11. from django.contrib.contenttypes.models import ContentType
  12. from django.db import models
  13. from django.utils.encoding import python_2_unicode_compatible
  14. @python_2_unicode_compatible
  15. class Category(models.Model):
  16. title = models.CharField(max_length=100)
  17. description = models.TextField()
  18. def __str__(self):
  19. return self.title
  20. class Meta:
  21. ordering = ('title',)
  22. @python_2_unicode_compatible
  23. class Article(models.Model):
  24. headline = models.CharField(max_length=100, default='Default headline')
  25. pub_date = models.DateTimeField()
  26. def __str__(self):
  27. return self.headline
  28. class Meta:
  29. ordering = ('-pub_date', 'headline')
  30. @python_2_unicode_compatible
  31. class Blog(models.Model):
  32. name = models.CharField(max_length=100)
  33. featured = models.ForeignKey(Article, models.CASCADE, related_name='fixtures_featured_set')
  34. articles = models.ManyToManyField(Article, blank=True,
  35. related_name='fixtures_articles_set')
  36. def __str__(self):
  37. return self.name
  38. @python_2_unicode_compatible
  39. class Tag(models.Model):
  40. name = models.CharField(max_length=100)
  41. tagged_type = models.ForeignKey(ContentType, models.CASCADE, related_name="fixtures_tag_set")
  42. tagged_id = models.PositiveIntegerField(default=0)
  43. tagged = GenericForeignKey(ct_field='tagged_type', fk_field='tagged_id')
  44. def __str__(self):
  45. return '<%s: %s> tagged "%s"' % (self.tagged.__class__.__name__,
  46. self.tagged, self.name)
  47. class PersonManager(models.Manager):
  48. def get_by_natural_key(self, name):
  49. return self.get(name=name)
  50. @python_2_unicode_compatible
  51. class Person(models.Model):
  52. objects = PersonManager()
  53. name = models.CharField(max_length=100)
  54. def __str__(self):
  55. return self.name
  56. class Meta:
  57. ordering = ('name',)
  58. def natural_key(self):
  59. return (self.name,)
  60. class SpyManager(PersonManager):
  61. def get_queryset(self):
  62. return super(SpyManager, self).get_queryset().filter(cover_blown=False)
  63. class Spy(Person):
  64. objects = SpyManager()
  65. cover_blown = models.BooleanField(default=False)
  66. @python_2_unicode_compatible
  67. class Visa(models.Model):
  68. person = models.ForeignKey(Person, models.CASCADE)
  69. permissions = models.ManyToManyField(Permission, blank=True)
  70. def __str__(self):
  71. return '%s %s' % (self.person.name,
  72. ', '.join(p.name for p in self.permissions.all()))
  73. @python_2_unicode_compatible
  74. class Book(models.Model):
  75. name = models.CharField(max_length=100)
  76. authors = models.ManyToManyField(Person)
  77. def __str__(self):
  78. authors = ' and '.join(a.name for a in self.authors.all())
  79. return '%s by %s' % (self.name, authors) if authors else self.name
  80. class Meta:
  81. ordering = ('name',)