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