tests.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. from __future__ import unicode_literals
  2. from django import forms
  3. from django.contrib.contenttypes.forms import generic_inlineformset_factory
  4. from django.contrib.contenttypes.models import ContentType
  5. from django.test import TestCase
  6. from django.utils import six
  7. from .models import (TaggedItem, ValuableTaggedItem, Comparison, Animal,
  8. Vegetable, Mineral, Gecko, Rock, ManualPK,
  9. ForProxyModelModel, ForConcreteModelModel,
  10. ProxyRelatedModel, ConcreteRelatedModel, AllowsNullGFK)
  11. class GenericRelationsTests(TestCase):
  12. def test_generic_relations(self):
  13. # Create the world in 7 lines of code...
  14. lion = Animal.objects.create(common_name="Lion", latin_name="Panthera leo")
  15. platypus = Animal.objects.create(
  16. common_name="Platypus", latin_name="Ornithorhynchus anatinus"
  17. )
  18. Vegetable.objects.create(name="Eggplant", is_yucky=True)
  19. bacon = Vegetable.objects.create(name="Bacon", is_yucky=False)
  20. quartz = Mineral.objects.create(name="Quartz", hardness=7)
  21. # Objects with declared GenericRelations can be tagged directly -- the
  22. # API mimics the many-to-many API.
  23. bacon.tags.create(tag="fatty")
  24. bacon.tags.create(tag="salty")
  25. lion.tags.create(tag="yellow")
  26. lion.tags.create(tag="hairy")
  27. platypus.tags.create(tag="fatty")
  28. self.assertQuerysetEqual(lion.tags.all(), [
  29. "<TaggedItem: hairy>",
  30. "<TaggedItem: yellow>"
  31. ])
  32. self.assertQuerysetEqual(bacon.tags.all(), [
  33. "<TaggedItem: fatty>",
  34. "<TaggedItem: salty>"
  35. ])
  36. # You can easily access the content object like a foreign key.
  37. t = TaggedItem.objects.get(tag="salty")
  38. self.assertEqual(t.content_object, bacon)
  39. # Recall that the Mineral class doesn't have an explicit GenericRelation
  40. # defined. That's OK, because you can create TaggedItems explicitly.
  41. tag1 = TaggedItem.objects.create(content_object=quartz, tag="shiny")
  42. TaggedItem.objects.create(content_object=quartz, tag="clearish")
  43. # However, excluding GenericRelations means your lookups have to be a
  44. # bit more explicit.
  45. ctype = ContentType.objects.get_for_model(quartz)
  46. q = TaggedItem.objects.filter(
  47. content_type__pk=ctype.id, object_id=quartz.id
  48. )
  49. self.assertQuerysetEqual(q, [
  50. "<TaggedItem: clearish>",
  51. "<TaggedItem: shiny>"
  52. ])
  53. # You can set a generic foreign key in the way you'd expect.
  54. tag1.content_object = platypus
  55. tag1.save()
  56. self.assertQuerysetEqual(platypus.tags.all(), [
  57. "<TaggedItem: fatty>",
  58. "<TaggedItem: shiny>"
  59. ])
  60. q = TaggedItem.objects.filter(
  61. content_type__pk=ctype.id, object_id=quartz.id
  62. )
  63. self.assertQuerysetEqual(q, ["<TaggedItem: clearish>"])
  64. # Queries across generic relations respect the content types. Even
  65. # though there are two TaggedItems with a tag of "fatty", this query
  66. # only pulls out the one with the content type related to Animals.
  67. self.assertQuerysetEqual(Animal.objects.order_by('common_name'), [
  68. "<Animal: Lion>",
  69. "<Animal: Platypus>"
  70. ])
  71. # Create another fatty tagged instance with different PK to ensure
  72. # there is a content type restriction in the generated queries below.
  73. mpk = ManualPK.objects.create(id=lion.pk)
  74. mpk.tags.create(tag="fatty")
  75. self.assertQuerysetEqual(Animal.objects.filter(tags__tag='fatty'), [
  76. "<Animal: Platypus>"
  77. ])
  78. self.assertQuerysetEqual(Animal.objects.exclude(tags__tag='fatty'), [
  79. "<Animal: Lion>"
  80. ])
  81. mpk.delete()
  82. # If you delete an object with an explicit Generic relation, the related
  83. # objects are deleted when the source object is deleted.
  84. # Original list of tags:
  85. comp_func = lambda obj: (
  86. obj.tag, obj.content_type.model_class(), obj.object_id
  87. )
  88. self.assertQuerysetEqual(TaggedItem.objects.all(), [
  89. ('clearish', Mineral, quartz.pk),
  90. ('fatty', Animal, platypus.pk),
  91. ('fatty', Vegetable, bacon.pk),
  92. ('hairy', Animal, lion.pk),
  93. ('salty', Vegetable, bacon.pk),
  94. ('shiny', Animal, platypus.pk),
  95. ('yellow', Animal, lion.pk)
  96. ],
  97. comp_func
  98. )
  99. lion.delete()
  100. self.assertQuerysetEqual(TaggedItem.objects.all(), [
  101. ('clearish', Mineral, quartz.pk),
  102. ('fatty', Animal, platypus.pk),
  103. ('fatty', Vegetable, bacon.pk),
  104. ('salty', Vegetable, bacon.pk),
  105. ('shiny', Animal, platypus.pk)
  106. ],
  107. comp_func
  108. )
  109. # If Generic Relation is not explicitly defined, any related objects
  110. # remain after deletion of the source object.
  111. quartz_pk = quartz.pk
  112. quartz.delete()
  113. self.assertQuerysetEqual(TaggedItem.objects.all(), [
  114. ('clearish', Mineral, quartz_pk),
  115. ('fatty', Animal, platypus.pk),
  116. ('fatty', Vegetable, bacon.pk),
  117. ('salty', Vegetable, bacon.pk),
  118. ('shiny', Animal, platypus.pk)
  119. ],
  120. comp_func
  121. )
  122. # If you delete a tag, the objects using the tag are unaffected
  123. # (other than losing a tag)
  124. tag = TaggedItem.objects.order_by("id")[0]
  125. tag.delete()
  126. self.assertQuerysetEqual(bacon.tags.all(), ["<TaggedItem: salty>"])
  127. self.assertQuerysetEqual(TaggedItem.objects.all(), [
  128. ('clearish', Mineral, quartz_pk),
  129. ('fatty', Animal, platypus.pk),
  130. ('salty', Vegetable, bacon.pk),
  131. ('shiny', Animal, platypus.pk)
  132. ],
  133. comp_func
  134. )
  135. TaggedItem.objects.filter(tag='fatty').delete()
  136. ctype = ContentType.objects.get_for_model(lion)
  137. self.assertQuerysetEqual(Animal.objects.filter(tags__content_type=ctype), [
  138. "<Animal: Platypus>"
  139. ])
  140. def test_multiple_gfk(self):
  141. # Simple tests for multiple GenericForeignKeys
  142. # only uses one model, since the above tests should be sufficient.
  143. tiger = Animal.objects.create(common_name="tiger")
  144. cheetah = Animal.objects.create(common_name="cheetah")
  145. bear = Animal.objects.create(common_name="bear")
  146. # Create directly
  147. Comparison.objects.create(
  148. first_obj=cheetah, other_obj=tiger, comparative="faster"
  149. )
  150. Comparison.objects.create(
  151. first_obj=tiger, other_obj=cheetah, comparative="cooler"
  152. )
  153. # Create using GenericRelation
  154. tiger.comparisons.create(other_obj=bear, comparative="cooler")
  155. tiger.comparisons.create(other_obj=cheetah, comparative="stronger")
  156. self.assertQuerysetEqual(cheetah.comparisons.all(), [
  157. "<Comparison: cheetah is faster than tiger>"
  158. ])
  159. # Filtering works
  160. self.assertQuerysetEqual(tiger.comparisons.filter(comparative="cooler"), [
  161. "<Comparison: tiger is cooler than cheetah>",
  162. "<Comparison: tiger is cooler than bear>",
  163. ], ordered=False)
  164. # Filtering and deleting works
  165. subjective = ["cooler"]
  166. tiger.comparisons.filter(comparative__in=subjective).delete()
  167. self.assertQuerysetEqual(Comparison.objects.all(), [
  168. "<Comparison: cheetah is faster than tiger>",
  169. "<Comparison: tiger is stronger than cheetah>"
  170. ], ordered=False)
  171. # If we delete cheetah, Comparisons with cheetah as 'first_obj' will be
  172. # deleted since Animal has an explicit GenericRelation to Comparison
  173. # through first_obj. Comparisons with cheetah as 'other_obj' will not
  174. # be deleted.
  175. cheetah.delete()
  176. self.assertQuerysetEqual(Comparison.objects.all(), [
  177. "<Comparison: tiger is stronger than None>"
  178. ])
  179. def test_gfk_subclasses(self):
  180. # GenericForeignKey should work with subclasses (see #8309)
  181. quartz = Mineral.objects.create(name="Quartz", hardness=7)
  182. valuedtag = ValuableTaggedItem.objects.create(
  183. content_object=quartz, tag="shiny", value=10
  184. )
  185. self.assertEqual(valuedtag.content_object, quartz)
  186. def test_generic_inline_formsets(self):
  187. GenericFormSet = generic_inlineformset_factory(TaggedItem, extra=1)
  188. formset = GenericFormSet()
  189. self.assertHTMLEqual(''.join(form.as_p() for form in formset.forms), """<p><label for="id_generic_relations-taggeditem-content_type-object_id-0-tag">Tag:</label> <input id="id_generic_relations-taggeditem-content_type-object_id-0-tag" type="text" name="generic_relations-taggeditem-content_type-object_id-0-tag" maxlength="50" /></p>
  190. <p><label for="id_generic_relations-taggeditem-content_type-object_id-0-DELETE">Delete:</label> <input type="checkbox" name="generic_relations-taggeditem-content_type-object_id-0-DELETE" id="id_generic_relations-taggeditem-content_type-object_id-0-DELETE" /><input type="hidden" name="generic_relations-taggeditem-content_type-object_id-0-id" id="id_generic_relations-taggeditem-content_type-object_id-0-id" /></p>""")
  191. formset = GenericFormSet(instance=Animal())
  192. self.assertHTMLEqual(''.join(form.as_p() for form in formset.forms), """<p><label for="id_generic_relations-taggeditem-content_type-object_id-0-tag">Tag:</label> <input id="id_generic_relations-taggeditem-content_type-object_id-0-tag" type="text" name="generic_relations-taggeditem-content_type-object_id-0-tag" maxlength="50" /></p>
  193. <p><label for="id_generic_relations-taggeditem-content_type-object_id-0-DELETE">Delete:</label> <input type="checkbox" name="generic_relations-taggeditem-content_type-object_id-0-DELETE" id="id_generic_relations-taggeditem-content_type-object_id-0-DELETE" /><input type="hidden" name="generic_relations-taggeditem-content_type-object_id-0-id" id="id_generic_relations-taggeditem-content_type-object_id-0-id" /></p>""")
  194. platypus = Animal.objects.create(
  195. common_name="Platypus", latin_name="Ornithorhynchus anatinus"
  196. )
  197. platypus.tags.create(tag="shiny")
  198. GenericFormSet = generic_inlineformset_factory(TaggedItem, extra=1)
  199. formset = GenericFormSet(instance=platypus)
  200. tagged_item_id = TaggedItem.objects.get(
  201. tag='shiny', object_id=platypus.id
  202. ).id
  203. self.assertHTMLEqual(''.join(form.as_p() for form in formset.forms), """<p><label for="id_generic_relations-taggeditem-content_type-object_id-0-tag">Tag:</label> <input id="id_generic_relations-taggeditem-content_type-object_id-0-tag" type="text" name="generic_relations-taggeditem-content_type-object_id-0-tag" value="shiny" maxlength="50" /></p>
  204. <p><label for="id_generic_relations-taggeditem-content_type-object_id-0-DELETE">Delete:</label> <input type="checkbox" name="generic_relations-taggeditem-content_type-object_id-0-DELETE" id="id_generic_relations-taggeditem-content_type-object_id-0-DELETE" /><input type="hidden" name="generic_relations-taggeditem-content_type-object_id-0-id" value="%s" id="id_generic_relations-taggeditem-content_type-object_id-0-id" /></p><p><label for="id_generic_relations-taggeditem-content_type-object_id-1-tag">Tag:</label> <input id="id_generic_relations-taggeditem-content_type-object_id-1-tag" type="text" name="generic_relations-taggeditem-content_type-object_id-1-tag" maxlength="50" /></p>
  205. <p><label for="id_generic_relations-taggeditem-content_type-object_id-1-DELETE">Delete:</label> <input type="checkbox" name="generic_relations-taggeditem-content_type-object_id-1-DELETE" id="id_generic_relations-taggeditem-content_type-object_id-1-DELETE" /><input type="hidden" name="generic_relations-taggeditem-content_type-object_id-1-id" id="id_generic_relations-taggeditem-content_type-object_id-1-id" /></p>""" % tagged_item_id)
  206. lion = Animal.objects.create(common_name="Lion", latin_name="Panthera leo")
  207. formset = GenericFormSet(instance=lion, prefix='x')
  208. self.assertHTMLEqual(''.join(form.as_p() for form in formset.forms), """<p><label for="id_x-0-tag">Tag:</label> <input id="id_x-0-tag" type="text" name="x-0-tag" maxlength="50" /></p>
  209. <p><label for="id_x-0-DELETE">Delete:</label> <input type="checkbox" name="x-0-DELETE" id="id_x-0-DELETE" /><input type="hidden" name="x-0-id" id="id_x-0-id" /></p>""")
  210. def test_gfk_manager(self):
  211. # GenericForeignKey should not use the default manager (which may filter objects) #16048
  212. tailless = Gecko.objects.create(has_tail=False)
  213. tag = TaggedItem.objects.create(content_object=tailless, tag="lizard")
  214. self.assertEqual(tag.content_object, tailless)
  215. def test_subclasses_with_gen_rel(self):
  216. """
  217. Test that concrete model subclasses with generic relations work
  218. correctly (ticket 11263).
  219. """
  220. granite = Rock.objects.create(name='granite', hardness=5)
  221. TaggedItem.objects.create(content_object=granite, tag="countertop")
  222. self.assertEqual(Rock.objects.filter(tags__tag="countertop").count(), 1)
  223. def test_generic_inline_formsets_initial(self):
  224. """
  225. Test for #17927 Initial values support for BaseGenericInlineFormSet.
  226. """
  227. quartz = Mineral.objects.create(name="Quartz", hardness=7)
  228. GenericFormSet = generic_inlineformset_factory(TaggedItem, extra=1)
  229. ctype = ContentType.objects.get_for_model(quartz)
  230. initial_data = [{
  231. 'tag': 'lizard',
  232. 'content_type': ctype.pk,
  233. 'object_id': quartz.pk,
  234. }]
  235. formset = GenericFormSet(initial=initial_data)
  236. self.assertEqual(formset.forms[0].initial, initial_data[0])
  237. def test_get_or_create(self):
  238. # get_or_create should work with virtual fields (content_object)
  239. quartz = Mineral.objects.create(name="Quartz", hardness=7)
  240. tag, created = TaggedItem.objects.get_or_create(tag="shiny",
  241. defaults={'content_object': quartz})
  242. self.assertTrue(created)
  243. self.assertEqual(tag.tag, "shiny")
  244. self.assertEqual(tag.content_object.id, quartz.id)
  245. def test_update_or_create_defaults(self):
  246. # update_or_create should work with virtual fields (content_object)
  247. quartz = Mineral.objects.create(name="Quartz", hardness=7)
  248. diamond = Mineral.objects.create(name="Diamond", hardness=7)
  249. tag, created = TaggedItem.objects.update_or_create(tag="shiny",
  250. defaults={'content_object': quartz})
  251. self.assertTrue(created)
  252. self.assertEqual(tag.content_object.id, quartz.id)
  253. tag, created = TaggedItem.objects.update_or_create(tag="shiny",
  254. defaults={'content_object': diamond})
  255. self.assertFalse(created)
  256. self.assertEqual(tag.content_object.id, diamond.id)
  257. class CustomWidget(forms.TextInput):
  258. pass
  259. class TaggedItemForm(forms.ModelForm):
  260. class Meta:
  261. model = TaggedItem
  262. fields = '__all__'
  263. widgets = {'tag': CustomWidget}
  264. class GenericInlineFormsetTest(TestCase):
  265. def test_generic_inlineformset_factory(self):
  266. """
  267. Regression for #14572: Using base forms with widgets
  268. defined in Meta should not raise errors.
  269. """
  270. Formset = generic_inlineformset_factory(TaggedItem, TaggedItemForm)
  271. form = Formset().forms[0]
  272. self.assertIsInstance(form['tag'].field.widget, CustomWidget)
  273. def test_save_new_uses_form_save(self):
  274. """
  275. Regression for #16260: save_new should call form.save()
  276. """
  277. class SaveTestForm(forms.ModelForm):
  278. def save(self, *args, **kwargs):
  279. self.instance.saved_by = "custom method"
  280. return super(SaveTestForm, self).save(*args, **kwargs)
  281. Formset = generic_inlineformset_factory(
  282. ForProxyModelModel, fields='__all__', form=SaveTestForm)
  283. instance = ProxyRelatedModel.objects.create()
  284. data = {
  285. 'form-TOTAL_FORMS': '1',
  286. 'form-INITIAL_FORMS': '0',
  287. 'form-MAX_NUM_FORMS': '',
  288. 'form-0-title': 'foo',
  289. }
  290. formset = Formset(data, instance=instance, prefix='form')
  291. self.assertTrue(formset.is_valid())
  292. new_obj = formset.save()[0]
  293. self.assertEqual(new_obj.saved_by, "custom method")
  294. def test_save_new_for_proxy(self):
  295. Formset = generic_inlineformset_factory(ForProxyModelModel,
  296. fields='__all__', for_concrete_model=False)
  297. instance = ProxyRelatedModel.objects.create()
  298. data = {
  299. 'form-TOTAL_FORMS': '1',
  300. 'form-INITIAL_FORMS': '0',
  301. 'form-MAX_NUM_FORMS': '',
  302. 'form-0-title': 'foo',
  303. }
  304. formset = Formset(data, instance=instance, prefix='form')
  305. self.assertTrue(formset.is_valid())
  306. new_obj, = formset.save()
  307. self.assertEqual(new_obj.obj, instance)
  308. def test_save_new_for_concrete(self):
  309. Formset = generic_inlineformset_factory(ForProxyModelModel,
  310. fields='__all__', for_concrete_model=True)
  311. instance = ProxyRelatedModel.objects.create()
  312. data = {
  313. 'form-TOTAL_FORMS': '1',
  314. 'form-INITIAL_FORMS': '0',
  315. 'form-MAX_NUM_FORMS': '',
  316. 'form-0-title': 'foo',
  317. }
  318. formset = Formset(data, instance=instance, prefix='form')
  319. self.assertTrue(formset.is_valid())
  320. new_obj, = formset.save()
  321. self.assertNotIsInstance(new_obj.obj, ProxyRelatedModel)
  322. class ProxyRelatedModelTest(TestCase):
  323. def test_default_behavior(self):
  324. """
  325. The default for for_concrete_model should be True
  326. """
  327. base = ForConcreteModelModel()
  328. base.obj = rel = ProxyRelatedModel.objects.create()
  329. base.save()
  330. base = ForConcreteModelModel.objects.get(pk=base.pk)
  331. rel = ConcreteRelatedModel.objects.get(pk=rel.pk)
  332. self.assertEqual(base.obj, rel)
  333. def test_works_normally(self):
  334. """
  335. When for_concrete_model is False, we should still be able to get
  336. an instance of the concrete class.
  337. """
  338. base = ForProxyModelModel()
  339. base.obj = rel = ConcreteRelatedModel.objects.create()
  340. base.save()
  341. base = ForProxyModelModel.objects.get(pk=base.pk)
  342. self.assertEqual(base.obj, rel)
  343. def test_proxy_is_returned(self):
  344. """
  345. Instances of the proxy should be returned when
  346. for_concrete_model is False.
  347. """
  348. base = ForProxyModelModel()
  349. base.obj = ProxyRelatedModel.objects.create()
  350. base.save()
  351. base = ForProxyModelModel.objects.get(pk=base.pk)
  352. self.assertIsInstance(base.obj, ProxyRelatedModel)
  353. def test_query(self):
  354. base = ForProxyModelModel()
  355. base.obj = rel = ConcreteRelatedModel.objects.create()
  356. base.save()
  357. self.assertEqual(rel, ConcreteRelatedModel.objects.get(bases__id=base.id))
  358. def test_query_proxy(self):
  359. base = ForProxyModelModel()
  360. base.obj = rel = ProxyRelatedModel.objects.create()
  361. base.save()
  362. self.assertEqual(rel, ProxyRelatedModel.objects.get(bases__id=base.id))
  363. def test_generic_relation(self):
  364. base = ForProxyModelModel()
  365. base.obj = ProxyRelatedModel.objects.create()
  366. base.save()
  367. base = ForProxyModelModel.objects.get(pk=base.pk)
  368. rel = ProxyRelatedModel.objects.get(pk=base.obj.pk)
  369. self.assertEqual(base, rel.bases.get())
  370. def test_generic_relation_set(self):
  371. base = ForProxyModelModel()
  372. base.obj = ConcreteRelatedModel.objects.create()
  373. base.save()
  374. newrel = ConcreteRelatedModel.objects.create()
  375. newrel.bases = [base]
  376. newrel = ConcreteRelatedModel.objects.get(pk=newrel.pk)
  377. self.assertEqual(base, newrel.bases.get())
  378. class TestInitWithNoneArgument(TestCase):
  379. def test_none_not_allowed(self):
  380. # TaggedItem requires a content_type, initializing with None should
  381. # raise a ValueError.
  382. with six.assertRaisesRegex(self, ValueError,
  383. 'Cannot assign None: "TaggedItem.content_type" does not allow null values'):
  384. TaggedItem(content_object=None)
  385. def test_none_allowed(self):
  386. # AllowsNullGFK doesn't require a content_type, so None argument should
  387. # also be allowed.
  388. AllowsNullGFK(content_object=None)