test_uuid.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from django.forms.models import inlineformset_factory
  2. from django.test import TestCase
  3. from .models import (
  4. AutoPKChildOfUUIDPKParent, AutoPKParent, ChildRelatedViaAK,
  5. ChildWithEditablePK, ParentWithUUIDAlternateKey, UUIDPKChild,
  6. UUIDPKChildOfAutoPKParent, UUIDPKParent,
  7. )
  8. class InlineFormsetTests(TestCase):
  9. def test_inlineformset_factory_nulls_default_pks(self):
  10. """
  11. #24377 - If we're adding a new object, a parent's auto-generated pk
  12. from the model field default should be ignored as it's regenerated on
  13. the save request.
  14. Tests the case where both the parent and child have a UUID primary key.
  15. """
  16. FormSet = inlineformset_factory(UUIDPKParent, UUIDPKChild, fields='__all__')
  17. formset = FormSet()
  18. self.assertIsNone(formset.forms[0].fields['parent'].initial)
  19. def test_inlineformset_factory_ignores_default_pks_on_submit(self):
  20. """
  21. #24377 - Inlines with a model field default should ignore that default
  22. value to avoid triggering validation on empty forms.
  23. """
  24. FormSet = inlineformset_factory(UUIDPKParent, UUIDPKChild, fields='__all__')
  25. formset = FormSet({
  26. 'uuidpkchild_set-TOTAL_FORMS': 3,
  27. 'uuidpkchild_set-INITIAL_FORMS': 0,
  28. 'uuidpkchild_set-MAX_NUM_FORMS': '',
  29. 'uuidpkchild_set-0-name': 'Foo',
  30. 'uuidpkchild_set-1-name': '',
  31. 'uuidpkchild_set-2-name': '',
  32. })
  33. self.assertTrue(formset.is_valid())
  34. def test_inlineformset_factory_nulls_default_pks_uuid_parent_auto_child(self):
  35. """
  36. #24958 - Variant of test_inlineformset_factory_nulls_default_pks for
  37. the case of a parent object with a UUID primary key and a child object
  38. with an AutoField primary key.
  39. """
  40. FormSet = inlineformset_factory(UUIDPKParent, AutoPKChildOfUUIDPKParent, fields='__all__')
  41. formset = FormSet()
  42. self.assertIsNone(formset.forms[0].fields['parent'].initial)
  43. def test_inlineformset_factory_nulls_default_pks_auto_parent_uuid_child(self):
  44. """
  45. #24958 - Variant of test_inlineformset_factory_nulls_default_pks for
  46. the case of a parent object with an AutoField primary key and a child
  47. object with a UUID primary key.
  48. """
  49. FormSet = inlineformset_factory(AutoPKParent, UUIDPKChildOfAutoPKParent, fields='__all__')
  50. formset = FormSet()
  51. self.assertIsNone(formset.forms[0].fields['parent'].initial)
  52. def test_inlineformset_factory_nulls_default_pks_child_editable_pk(self):
  53. """
  54. #24958 - Variant of test_inlineformset_factory_nulls_default_pks for
  55. the case of a parent object with a UUID primary key and a child
  56. object with an editable natural key for a primary key.
  57. """
  58. FormSet = inlineformset_factory(UUIDPKParent, ChildWithEditablePK, fields='__all__')
  59. formset = FormSet()
  60. self.assertIsNone(formset.forms[0].fields['parent'].initial)
  61. def test_inlineformset_factory_nulls_default_pks_alternate_key_relation(self):
  62. """
  63. #24958 - Variant of test_inlineformset_factory_nulls_default_pks for
  64. the case of a parent object with a UUID alternate key and a child
  65. object that relates to that alternate key.
  66. """
  67. FormSet = inlineformset_factory(ParentWithUUIDAlternateKey, ChildRelatedViaAK, fields='__all__')
  68. formset = FormSet()
  69. self.assertIsNone(formset.forms[0].fields['parent'].initial)