models.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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, on 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 Visa(models.Model):
  57. person = models.ForeignKey(Person)
  58. permissions = models.ManyToManyField(Permission, blank=True)
  59. def __unicode__(self):
  60. return '%s %s' % (self.person.name,
  61. ', '.join(p.name for p in self.permissions.all()))
  62. class Book(models.Model):
  63. name = models.CharField(max_length=100)
  64. authors = models.ManyToManyField(Person)
  65. def __unicode__(self):
  66. return '%s by %s' % (self.name,
  67. ' and '.join(a.name for a in self.authors.all()))
  68. class Meta:
  69. ordering = ('name',)
  70. __test__ = {'API_TESTS': """
  71. >>> from django.core import management
  72. >>> from django.db.models import get_app
  73. # Reset the database representation of this app.
  74. # This will return the database to a clean initial state.
  75. >>> management.call_command('flush', verbosity=0, interactive=False)
  76. # Syncdb introduces 1 initial data object from initial_data.json.
  77. >>> Article.objects.all()
  78. [<Article: Python program becomes self aware>]
  79. # Load fixture 1. Single JSON file, with two objects.
  80. >>> management.call_command('loaddata', 'fixture1.json', verbosity=0)
  81. >>> Article.objects.all()
  82. [<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
  83. # Dump the current contents of the database as a JSON fixture
  84. >>> management.call_command('dumpdata', 'fixtures', format='json')
  85. [{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]
  86. # Try just dumping the contents of fixtures.Category
  87. >>> management.call_command('dumpdata', 'fixtures.Category', format='json')
  88. [{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}]
  89. # ...and just fixtures.Article
  90. >>> management.call_command('dumpdata', 'fixtures.Article', format='json')
  91. [{"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]
  92. # ...and both
  93. >>> management.call_command('dumpdata', 'fixtures.Category', 'fixtures.Article', format='json')
  94. [{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]
  95. # Specify a specific model twice
  96. >>> management.call_command('dumpdata', 'fixtures.Article', 'fixtures.Article', format='json')
  97. [{"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]
  98. # Specify a dump that specifies Article both explicitly and implicitly
  99. >>> management.call_command('dumpdata', 'fixtures.Article', 'fixtures', format='json')
  100. [{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]
  101. # Same again, but specify in the reverse order
  102. >>> management.call_command('dumpdata', 'fixtures', 'fixtures.Article', format='json')
  103. [{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]
  104. # Specify one model from one application, and an entire other application.
  105. >>> management.call_command('dumpdata', 'fixtures.Category', 'sites', format='json')
  106. [{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 1, "model": "sites.site", "fields": {"domain": "example.com", "name": "example.com"}}]
  107. # Load fixture 2. JSON file imported by default. Overwrites some existing objects
  108. >>> management.call_command('loaddata', 'fixture2.json', verbosity=0)
  109. >>> Article.objects.all()
  110. [<Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
  111. # Load fixture 3, XML format.
  112. >>> management.call_command('loaddata', 'fixture3.xml', verbosity=0)
  113. >>> Article.objects.all()
  114. [<Article: XML identified as leading cause of cancer>, <Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker on TV is great!>, <Article: Python program becomes self aware>]
  115. # Load fixture 6, JSON file with dynamic ContentType fields. Testing ManyToOne.
  116. >>> management.call_command('loaddata', 'fixture6.json', verbosity=0)
  117. >>> Tag.objects.all()
  118. [<Tag: <Article: Copyright is fine the way it is> tagged "copyright">, <Tag: <Article: Copyright is fine the way it is> tagged "law">]
  119. # Load fixture 7, XML file with dynamic ContentType fields. Testing ManyToOne.
  120. >>> management.call_command('loaddata', 'fixture7.xml', verbosity=0)
  121. >>> Tag.objects.all()
  122. [<Tag: <Article: Copyright is fine the way it is> tagged "copyright">, <Tag: <Article: Copyright is fine the way it is> tagged "legal">, <Tag: <Article: Django conquers world!> tagged "django">, <Tag: <Article: Django conquers world!> tagged "world domination">]
  123. # Load fixture 8, JSON file with dynamic Permission fields. Testing ManyToMany.
  124. >>> management.call_command('loaddata', 'fixture8.json', verbosity=0)
  125. >>> Visa.objects.all()
  126. [<Visa: Django Reinhardt Can add user, Can change user, Can delete user>, <Visa: Stephane Grappelli Can add user>, <Visa: Prince >]
  127. # Load fixture 9, XML file with dynamic Permission fields. Testing ManyToMany.
  128. >>> management.call_command('loaddata', 'fixture9.xml', verbosity=0)
  129. >>> Visa.objects.all()
  130. [<Visa: Django Reinhardt Can add user, Can change user, Can delete user>, <Visa: Stephane Grappelli Can add user, Can delete user>, <Visa: Artist formerly known as "Prince" Can change user>]
  131. >>> Book.objects.all()
  132. [<Book: Music for all ages by Artist formerly known as "Prince" and Django Reinhardt>]
  133. # Load a fixture that doesn't exist
  134. >>> management.call_command('loaddata', 'unknown.json', verbosity=0)
  135. # object list is unaffected
  136. >>> Article.objects.all()
  137. [<Article: XML identified as leading cause of cancer>, <Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker on TV is great!>, <Article: Python program becomes self aware>]
  138. # By default, you get raw keys on dumpdata
  139. >>> management.call_command('dumpdata', 'fixtures.book', format='json')
  140. [{"pk": 1, "model": "fixtures.book", "fields": {"name": "Music for all ages", "authors": [3, 1]}}]
  141. # But you can get natural keys if you ask for them and they are available
  142. >>> management.call_command('dumpdata', 'fixtures.book', format='json', use_natural_keys=True)
  143. [{"pk": 1, "model": "fixtures.book", "fields": {"name": "Music for all ages", "authors": [["Artist formerly known as \\"Prince\\""], ["Django Reinhardt"]]}}]
  144. # Dump the current contents of the database as a JSON fixture
  145. >>> management.call_command('dumpdata', 'fixtures', format='json', use_natural_keys=True)
  146. [{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 5, "model": "fixtures.article", "fields": {"headline": "XML identified as leading cause of cancer", "pub_date": "2006-06-16 16:00:00"}}, {"pk": 4, "model": "fixtures.article", "fields": {"headline": "Django conquers world!", "pub_date": "2006-06-16 15:00:00"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Copyright is fine the way it is", "pub_date": "2006-06-16 14:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker on TV is great!", "pub_date": "2006-06-16 11:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}, {"pk": 1, "model": "fixtures.tag", "fields": {"tagged_type": ["fixtures", "article"], "name": "copyright", "tagged_id": 3}}, {"pk": 2, "model": "fixtures.tag", "fields": {"tagged_type": ["fixtures", "article"], "name": "legal", "tagged_id": 3}}, {"pk": 3, "model": "fixtures.tag", "fields": {"tagged_type": ["fixtures", "article"], "name": "django", "tagged_id": 4}}, {"pk": 4, "model": "fixtures.tag", "fields": {"tagged_type": ["fixtures", "article"], "name": "world domination", "tagged_id": 4}}, {"pk": 3, "model": "fixtures.person", "fields": {"name": "Artist formerly known as \\"Prince\\""}}, {"pk": 1, "model": "fixtures.person", "fields": {"name": "Django Reinhardt"}}, {"pk": 2, "model": "fixtures.person", "fields": {"name": "Stephane Grappelli"}}, {"pk": 1, "model": "fixtures.visa", "fields": {"person": ["Django Reinhardt"], "permissions": [["add_user", "auth", "user"], ["change_user", "auth", "user"], ["delete_user", "auth", "user"]]}}, {"pk": 2, "model": "fixtures.visa", "fields": {"person": ["Stephane Grappelli"], "permissions": [["add_user", "auth", "user"], ["delete_user", "auth", "user"]]}}, {"pk": 3, "model": "fixtures.visa", "fields": {"person": ["Artist formerly known as \\"Prince\\""], "permissions": [["change_user", "auth", "user"]]}}, {"pk": 1, "model": "fixtures.book", "fields": {"name": "Music for all ages", "authors": [["Artist formerly known as \\"Prince\\""], ["Django Reinhardt"]]}}]
  147. # Dump the current contents of the database as an XML fixture
  148. >>> management.call_command('dumpdata', 'fixtures', format='xml', use_natural_keys=True)
  149. <?xml version="1.0" encoding="utf-8"?>
  150. <django-objects version="1.0"><object pk="1" model="fixtures.category"><field type="CharField" name="title">News Stories</field><field type="TextField" name="description">Latest news stories</field></object><object pk="5" model="fixtures.article"><field type="CharField" name="headline">XML identified as leading cause of cancer</field><field type="DateTimeField" name="pub_date">2006-06-16 16:00:00</field></object><object pk="4" model="fixtures.article"><field type="CharField" name="headline">Django conquers world!</field><field type="DateTimeField" name="pub_date">2006-06-16 15:00:00</field></object><object pk="3" model="fixtures.article"><field type="CharField" name="headline">Copyright is fine the way it is</field><field type="DateTimeField" name="pub_date">2006-06-16 14:00:00</field></object><object pk="2" model="fixtures.article"><field type="CharField" name="headline">Poker on TV is great!</field><field type="DateTimeField" name="pub_date">2006-06-16 11:00:00</field></object><object pk="1" model="fixtures.article"><field type="CharField" name="headline">Python program becomes self aware</field><field type="DateTimeField" name="pub_date">2006-06-16 11:00:00</field></object><object pk="1" model="fixtures.tag"><field type="CharField" name="name">copyright</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">3</field></object><object pk="2" model="fixtures.tag"><field type="CharField" name="name">legal</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">3</field></object><object pk="3" model="fixtures.tag"><field type="CharField" name="name">django</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">4</field></object><object pk="4" model="fixtures.tag"><field type="CharField" name="name">world domination</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">4</field></object><object pk="3" model="fixtures.person"><field type="CharField" name="name">Artist formerly known as "Prince"</field></object><object pk="1" model="fixtures.person"><field type="CharField" name="name">Django Reinhardt</field></object><object pk="2" model="fixtures.person"><field type="CharField" name="name">Stephane Grappelli</field></object><object pk="1" model="fixtures.visa"><field to="fixtures.person" name="person" rel="ManyToOneRel"><natural>Django Reinhardt</natural></field><field to="auth.permission" name="permissions" rel="ManyToManyRel"><object><natural>add_user</natural><natural>auth</natural><natural>user</natural></object><object><natural>change_user</natural><natural>auth</natural><natural>user</natural></object><object><natural>delete_user</natural><natural>auth</natural><natural>user</natural></object></field></object><object pk="2" model="fixtures.visa"><field to="fixtures.person" name="person" rel="ManyToOneRel"><natural>Stephane Grappelli</natural></field><field to="auth.permission" name="permissions" rel="ManyToManyRel"><object><natural>add_user</natural><natural>auth</natural><natural>user</natural></object><object><natural>delete_user</natural><natural>auth</natural><natural>user</natural></object></field></object><object pk="3" model="fixtures.visa"><field to="fixtures.person" name="person" rel="ManyToOneRel"><natural>Artist formerly known as "Prince"</natural></field><field to="auth.permission" name="permissions" rel="ManyToManyRel"><object><natural>change_user</natural><natural>auth</natural><natural>user</natural></object></field></object><object pk="1" model="fixtures.book"><field type="CharField" name="name">Music for all ages</field><field to="fixtures.person" name="authors" rel="ManyToManyRel"><object><natural>Artist formerly known as "Prince"</natural></object><object><natural>Django Reinhardt</natural></object></field></object></django-objects>
  151. """}
  152. # Database flushing does not work on MySQL with the default storage engine
  153. # because it requires transaction support.
  154. if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.mysql':
  155. __test__['API_TESTS'] += \
  156. """
  157. # Reset the database representation of this app. This will delete all data.
  158. >>> management.call_command('flush', verbosity=0, interactive=False)
  159. >>> Article.objects.all()
  160. [<Article: Python program becomes self aware>]
  161. # Load fixture 1 again, using format discovery
  162. >>> management.call_command('loaddata', 'fixture1', verbosity=0)
  163. >>> Article.objects.all()
  164. [<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
  165. # Try to load fixture 2 using format discovery; this will fail
  166. # because there are two fixture2's in the fixtures directory
  167. >>> management.call_command('loaddata', 'fixture2', verbosity=0) # doctest: +ELLIPSIS
  168. Multiple fixtures named 'fixture2' in '...fixtures'. Aborting.
  169. # object list is unaffected
  170. >>> Article.objects.all()
  171. [<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
  172. # Dump the current contents of the database as a JSON fixture
  173. >>> management.call_command('dumpdata', 'fixtures', format='json')
  174. [{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]
  175. # Load fixture 4 (compressed), using format discovery
  176. >>> management.call_command('loaddata', 'fixture4', verbosity=0)
  177. >>> Article.objects.all()
  178. [<Article: Django pets kitten>, <Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
  179. >>> management.call_command('flush', verbosity=0, interactive=False)
  180. # Load fixture 4 (compressed), using format specification
  181. >>> management.call_command('loaddata', 'fixture4.json', verbosity=0)
  182. >>> Article.objects.all()
  183. [<Article: Django pets kitten>, <Article: Python program becomes self aware>]
  184. >>> management.call_command('flush', verbosity=0, interactive=False)
  185. # Load fixture 5 (compressed), using format *and* compression specification
  186. >>> management.call_command('loaddata', 'fixture5.json.zip', verbosity=0)
  187. >>> Article.objects.all()
  188. [<Article: WoW subscribers now outnumber readers>, <Article: Python program becomes self aware>]
  189. >>> management.call_command('flush', verbosity=0, interactive=False)
  190. # Load fixture 5 (compressed), only compression specification
  191. >>> management.call_command('loaddata', 'fixture5.zip', verbosity=0)
  192. >>> Article.objects.all()
  193. [<Article: WoW subscribers now outnumber readers>, <Article: Python program becomes self aware>]
  194. >>> management.call_command('flush', verbosity=0, interactive=False)
  195. # Try to load fixture 5 using format and compression discovery; this will fail
  196. # because there are two fixture5's in the fixtures directory
  197. >>> management.call_command('loaddata', 'fixture5', verbosity=0) # doctest: +ELLIPSIS
  198. Multiple fixtures named 'fixture5' in '...fixtures'. Aborting.
  199. >>> management.call_command('flush', verbosity=0, interactive=False)
  200. # Load db fixtures 1 and 2. These will load using the 'default' database identifier implicitly
  201. >>> management.call_command('loaddata', 'db_fixture_1', verbosity=0)
  202. >>> management.call_command('loaddata', 'db_fixture_2', verbosity=0)
  203. >>> Article.objects.all()
  204. [<Article: Who needs more than one database?>, <Article: Who needs to use compressed data?>, <Article: Python program becomes self aware>]
  205. >>> management.call_command('flush', verbosity=0, interactive=False)
  206. # Load db fixtures 1 and 2. These will load using the 'default' database identifier explicitly
  207. >>> management.call_command('loaddata', 'db_fixture_1', verbosity=0, using='default')
  208. >>> management.call_command('loaddata', 'db_fixture_2', verbosity=0, using='default')
  209. >>> Article.objects.all()
  210. [<Article: Who needs more than one database?>, <Article: Who needs to use compressed data?>, <Article: Python program becomes self aware>]
  211. >>> management.call_command('flush', verbosity=0, interactive=False)
  212. # Try to load db fixture 3. This won't load because the database identifier doesn't match
  213. >>> management.call_command('loaddata', 'db_fixture_3', verbosity=0)
  214. >>> Article.objects.all()
  215. [<Article: Python program becomes self aware>]
  216. >>> management.call_command('loaddata', 'db_fixture_3', verbosity=0, using='default')
  217. >>> Article.objects.all()
  218. [<Article: Python program becomes self aware>]
  219. >>> management.call_command('flush', verbosity=0, interactive=False)
  220. # Load back in fixture 1, we need the articles from it
  221. >>> management.call_command('loaddata', 'fixture1', verbosity=0)
  222. # Try to load fixture 6 using format discovery
  223. >>> management.call_command('loaddata', 'fixture6', verbosity=0)
  224. >>> Tag.objects.all()
  225. [<Tag: <Article: Time to reform copyright> tagged "copyright">, <Tag: <Article: Time to reform copyright> tagged "law">]
  226. # Dump the current contents of the database as a JSON fixture
  227. >>> management.call_command('dumpdata', 'fixtures', format='json', use_natural_keys=True)
  228. [{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}, {"pk": 1, "model": "fixtures.tag", "fields": {"tagged_type": ["fixtures", "article"], "name": "copyright", "tagged_id": 3}}, {"pk": 2, "model": "fixtures.tag", "fields": {"tagged_type": ["fixtures", "article"], "name": "law", "tagged_id": 3}}, {"pk": 1, "model": "fixtures.person", "fields": {"name": "Django Reinhardt"}}, {"pk": 3, "model": "fixtures.person", "fields": {"name": "Prince"}}, {"pk": 2, "model": "fixtures.person", "fields": {"name": "Stephane Grappelli"}}]
  229. # Dump the current contents of the database as an XML fixture
  230. >>> management.call_command('dumpdata', 'fixtures', format='xml', use_natural_keys=True)
  231. <?xml version="1.0" encoding="utf-8"?>
  232. <django-objects version="1.0"><object pk="1" model="fixtures.category"><field type="CharField" name="title">News Stories</field><field type="TextField" name="description">Latest news stories</field></object><object pk="3" model="fixtures.article"><field type="CharField" name="headline">Time to reform copyright</field><field type="DateTimeField" name="pub_date">2006-06-16 13:00:00</field></object><object pk="2" model="fixtures.article"><field type="CharField" name="headline">Poker has no place on ESPN</field><field type="DateTimeField" name="pub_date">2006-06-16 12:00:00</field></object><object pk="1" model="fixtures.article"><field type="CharField" name="headline">Python program becomes self aware</field><field type="DateTimeField" name="pub_date">2006-06-16 11:00:00</field></object><object pk="1" model="fixtures.tag"><field type="CharField" name="name">copyright</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">3</field></object><object pk="2" model="fixtures.tag"><field type="CharField" name="name">law</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">3</field></object><object pk="1" model="fixtures.person"><field type="CharField" name="name">Django Reinhardt</field></object><object pk="3" model="fixtures.person"><field type="CharField" name="name">Prince</field></object><object pk="2" model="fixtures.person"><field type="CharField" name="name">Stephane Grappelli</field></object></django-objects>
  233. """
  234. from django.test import TestCase
  235. class SampleTestCase(TestCase):
  236. fixtures = ['fixture1.json', 'fixture2.json']
  237. def testClassFixtures(self):
  238. "Check that test case has installed 4 fixture objects"
  239. self.assertEqual(Article.objects.count(), 4)
  240. self.assertEquals(str(Article.objects.all()), "[<Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]")