PageRenderTime 40ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/modeltests/fixtures/models.py

https://code.google.com/p/mango-py/
Python | 100 lines | 95 code | 0 blank | 5 comment | 1 complexity | 62c7d04fd5b433db62af63676d6dfc0a MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """
  2. 37. 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 import generic
  11. from django.contrib.contenttypes.models import ContentType
  12. from django.db import models, DEFAULT_DB_ALIAS
  13. from django.conf import settings
  14. class Category(models.Model):
  15. title = models.CharField(max_length=100)
  16. description = models.TextField()
  17. def __unicode__(self):
  18. return self.title
  19. class Meta:
  20. ordering = ('title',)
  21. class Article(models.Model):
  22. headline = models.CharField(max_length=100, default='Default headline')
  23. pub_date = models.DateTimeField()
  24. def __unicode__(self):
  25. return self.headline
  26. class Meta:
  27. ordering = ('-pub_date', 'headline')
  28. class Blog(models.Model):
  29. name = models.CharField(max_length=100)
  30. featured = models.ForeignKey(Article, related_name='fixtures_featured_set')
  31. articles = models.ManyToManyField(Article, blank=True,
  32. related_name='fixtures_articles_set')
  33. def __unicode__(self):
  34. return self.name
  35. class Tag(models.Model):
  36. name = models.CharField(max_length=100)
  37. tagged_type = models.ForeignKey(ContentType, related_name="fixtures_tag_set")
  38. tagged_id = models.PositiveIntegerField(default=0)
  39. tagged = generic.GenericForeignKey(ct_field='tagged_type',
  40. fk_field='tagged_id')
  41. def __unicode__(self):
  42. return '<%s: %s> tagged "%s"' % (self.tagged.__class__.__name__,
  43. self.tagged, self.name)
  44. class PersonManager(models.Manager):
  45. def get_by_natural_key(self, name):
  46. return self.get(name=name)
  47. class Person(models.Model):
  48. objects = PersonManager()
  49. name = models.CharField(max_length=100)
  50. def __unicode__(self):
  51. return self.name
  52. class Meta:
  53. ordering = ('name',)
  54. def natural_key(self):
  55. return (self.name,)
  56. class SpyManager(PersonManager):
  57. def get_query_set(self):
  58. return super(SpyManager, self).get_query_set().filter(cover_blown=False)
  59. class Spy(Person):
  60. objects = SpyManager()
  61. cover_blown = models.BooleanField(default=False)
  62. class Visa(models.Model):
  63. person = models.ForeignKey(Person)
  64. permissions = models.ManyToManyField(Permission, blank=True)
  65. def __unicode__(self):
  66. return '%s %s' % (self.person.name,
  67. ', '.join(p.name for p in self.permissions.all()))
  68. class Book(models.Model):
  69. name = models.CharField(max_length=100)
  70. authors = models.ManyToManyField(Person)
  71. def __unicode__(self):
  72. return '%s by %s' % (self.name,
  73. ' and '.join(a.name for a in self.authors.all()))
  74. class Meta:
  75. ordering = ('name',)