models.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. from django.db import models
  2. from django.contrib.auth.models import User
  3. from django.conf import settings
  4. import os
  5. class Animal(models.Model):
  6. name = models.CharField(max_length=150)
  7. latin_name = models.CharField(max_length=150)
  8. count = models.IntegerField()
  9. weight = models.FloatField()
  10. # use a non-default name for the default manager
  11. specimens = models.Manager()
  12. def __unicode__(self):
  13. return self.common_name
  14. def animal_pre_save_check(signal, sender, instance, **kwargs):
  15. "A signal that is used to check the type of data loaded from fixtures"
  16. print 'Count = %s (%s)' % (instance.count, type(instance.count))
  17. print 'Weight = %s (%s)' % (instance.weight, type(instance.weight))
  18. class Plant(models.Model):
  19. name = models.CharField(max_length=150)
  20. class Meta:
  21. # For testing when upper case letter in app name; regression for #4057
  22. db_table = "Fixtures_regress_plant"
  23. class Stuff(models.Model):
  24. name = models.CharField(max_length=20, null=True)
  25. owner = models.ForeignKey(User, null=True)
  26. def __unicode__(self):
  27. # Oracle doesn't distinguish between None and the empty string.
  28. # This hack makes the test case pass using Oracle.
  29. name = self.name
  30. if settings.DATABASE_ENGINE == 'oracle' and name == u'':
  31. name = None
  32. return unicode(name) + u' is owned by ' + unicode(self.owner)
  33. class Absolute(models.Model):
  34. name = models.CharField(max_length=40)
  35. load_count = 0
  36. def __init__(self, *args, **kwargs):
  37. super(Absolute, self).__init__(*args, **kwargs)
  38. Absolute.load_count += 1
  39. class Parent(models.Model):
  40. name = models.CharField(max_length=10)
  41. class Child(Parent):
  42. data = models.CharField(max_length=10)
  43. # Models to regression test #7572
  44. class Channel(models.Model):
  45. name = models.CharField(max_length=255)
  46. class Article(models.Model):
  47. title = models.CharField(max_length=255)
  48. channels = models.ManyToManyField(Channel)
  49. class Meta:
  50. ordering = ('id',)
  51. # Models to regression test #11428
  52. class Widget(models.Model):
  53. name = models.CharField(max_length=255)
  54. class WidgetProxy(Widget):
  55. class Meta:
  56. proxy = True
  57. __test__ = {'API_TESTS':"""
  58. >>> from django.core import management
  59. # Load a fixture that uses PK=1
  60. >>> management.call_command('loaddata', 'sequence', verbosity=0)
  61. # Create a new animal. Without a sequence reset, this new object
  62. # will take a PK of 1 (on Postgres), and the save will fail.
  63. # This is a regression test for ticket #3790.
  64. >>> animal = Animal(name='Platypus', latin_name='Ornithorhynchus anatinus', count=2, weight=2.3)
  65. >>> animal.save()
  66. ###############################################
  67. # Regression test for ticket #4558 -- pretty printing of XML fixtures
  68. # doesn't affect parsing of None values.
  69. # Load a pretty-printed XML fixture with Nulls.
  70. >>> management.call_command('loaddata', 'pretty.xml', verbosity=0)
  71. >>> Stuff.objects.all()
  72. [<Stuff: None is owned by None>]
  73. ###############################################
  74. # Regression test for ticket #6436 --
  75. # os.path.join will throw away the initial parts of a path if it encounters
  76. # an absolute path. This means that if a fixture is specified as an absolute path,
  77. # we need to make sure we don't discover the absolute path in every fixture directory.
  78. >>> load_absolute_path = os.path.join(os.path.dirname(__file__), 'fixtures', 'absolute.json')
  79. >>> management.call_command('loaddata', load_absolute_path, verbosity=0)
  80. >>> Absolute.load_count
  81. 1
  82. ###############################################
  83. # Test for ticket #4371 -- fixture loading fails silently in testcases
  84. # Validate that error conditions are caught correctly
  85. # redirect stderr for the next few tests...
  86. >>> import sys
  87. >>> savestderr = sys.stderr
  88. >>> sys.stderr = sys.stdout
  89. # Loading data of an unknown format should fail
  90. >>> management.call_command('loaddata', 'bad_fixture1.unkn', verbosity=0)
  91. Problem installing fixture 'bad_fixture1': unkn is not a known serialization format.
  92. # Loading a fixture file with invalid data using explicit filename
  93. >>> management.call_command('loaddata', 'bad_fixture2.xml', verbosity=0)
  94. No fixture data found for 'bad_fixture2'. (File format may be invalid.)
  95. # Loading a fixture file with invalid data without file extension
  96. >>> management.call_command('loaddata', 'bad_fixture2', verbosity=0)
  97. No fixture data found for 'bad_fixture2'. (File format may be invalid.)
  98. # Loading a fixture file with no data returns an error
  99. >>> management.call_command('loaddata', 'empty', verbosity=0)
  100. No fixture data found for 'empty'. (File format may be invalid.)
  101. # If any of the fixtures contain an error, loading is aborted
  102. # (Regression for #9011 - error message is correct)
  103. >>> management.call_command('loaddata', 'bad_fixture2', 'animal', verbosity=0)
  104. No fixture data found for 'bad_fixture2'. (File format may be invalid.)
  105. >>> sys.stderr = savestderr
  106. ###############################################
  107. # Test for ticket #7565 -- PostgreSQL sequence resetting checks shouldn't
  108. # ascend to parent models when inheritance is used (since they are treated
  109. # individually).
  110. >>> management.call_command('loaddata', 'model-inheritance.json', verbosity=0)
  111. ###############################################
  112. # Test for ticket #7572 -- MySQL has a problem if the same connection is
  113. # used to create tables, load data, and then query over that data.
  114. # To compensate, we close the connection after running loaddata.
  115. # This ensures that a new connection is opened when test queries are issued.
  116. >>> management.call_command('loaddata', 'big-fixture.json', verbosity=0)
  117. >>> articles = Article.objects.exclude(id=9)
  118. >>> articles.values_list('id', flat=True)
  119. [1, 2, 3, 4, 5, 6, 7, 8]
  120. # Just for good measure, run the same query again. Under the influence of
  121. # ticket #7572, this will give a different result to the previous call.
  122. >>> articles.values_list('id', flat=True)
  123. [1, 2, 3, 4, 5, 6, 7, 8]
  124. ###############################################
  125. # Test for tickets #8298, #9942 - Field values should be coerced into the
  126. # correct type by the deserializer, not as part of the database write.
  127. >>> models.signals.pre_save.connect(animal_pre_save_check)
  128. >>> management.call_command('loaddata', 'animal.xml', verbosity=0)
  129. Count = 42 (<type 'int'>)
  130. Weight = 1.2 (<type 'float'>)
  131. >>> models.signals.pre_save.disconnect(animal_pre_save_check)
  132. ###############################################
  133. # Regression for #11286 -- Ensure that dumpdata honors the default manager
  134. # Dump the current contents of the database as a JSON fixture
  135. >>> management.call_command('dumpdata', 'fixtures_regress.animal', format='json')
  136. [{"pk": 1, "model": "fixtures_regress.animal", "fields": {"count": 3, "weight": 1.2, "name": "Lion", "latin_name": "Panthera leo"}}, {"pk": 2, "model": "fixtures_regress.animal", "fields": {"count": 2, "weight": 2.29..., "name": "Platypus", "latin_name": "Ornithorhynchus anatinus"}}, {"pk": 10, "model": "fixtures_regress.animal", "fields": {"count": 42, "weight": 1.2, "name": "Emu", "latin_name": "Dromaius novaehollandiae"}}]
  137. ###############################################
  138. # Regression for #11428 - Proxy models aren't included
  139. # when you run dumpdata over an entire app
  140. # Flush out the database first
  141. >>> management.call_command('reset', 'fixtures_regress', interactive=False, verbosity=0)
  142. # Create an instance of the concrete class
  143. >>> Widget(name='grommet').save()
  144. # Dump data for the entire app. The proxy class shouldn't be included
  145. >>> management.call_command('dumpdata', 'fixtures_regress', format='json')
  146. [{"pk": 1, "model": "fixtures_regress.widget", "fields": {"name": "grommet"}}]
  147. """}