|
1 | | -from django.test import TestCase |
2 | | - |
| 1 | +from django import forms |
3 | 2 | from django.db import models |
4 | 3 | from django.db.models import functions |
5 | | -from polymorphic.models import PolymorphicTypeInvalid |
| 4 | +from polymorphic.models import PolymorphicModel, PolymorphicTypeInvalid |
6 | 5 | from polymorphic.tests.models import ( |
7 | 6 | Bottom, |
8 | 7 | Middle, |
|
16 | 15 | RelationBase, |
17 | 16 | RelationA, |
18 | 17 | RelationB, |
| 18 | + SpecialBook, |
| 19 | + Book, |
19 | 20 | ) |
| 21 | +from django.test import TestCase |
| 22 | +from django.contrib.contenttypes.models import ContentType |
| 23 | +from polymorphic.formsets import polymorphic_modelformset_factory, PolymorphicFormSetChild |
20 | 24 |
|
21 | 25 |
|
22 | 26 | class RegressionTests(TestCase): |
@@ -321,3 +325,118 @@ def test_issue_252_abstract_base_class(self): |
321 | 325 | self.assertIsInstance(relations[0], RelationBase) |
322 | 326 | self.assertIsInstance(relations[1], RelationA) |
323 | 327 | self.assertIsInstance(relations[2], RelationB) |
| 328 | + |
| 329 | + |
| 330 | +class SpecialBookForm(forms.ModelForm): |
| 331 | + class Meta: |
| 332 | + model = SpecialBook |
| 333 | + exclude = ("author",) |
| 334 | + |
| 335 | + |
| 336 | +class TestFormsetExclude(TestCase): |
| 337 | + def test_formset_child_respects_exclude(self): |
| 338 | + SpecialBookFormSet = polymorphic_modelformset_factory( |
| 339 | + Book, |
| 340 | + fields=[], |
| 341 | + formset_children=(PolymorphicFormSetChild(SpecialBook, form=SpecialBookForm),), |
| 342 | + ) |
| 343 | + formset = SpecialBookFormSet(queryset=SpecialBook.objects.none()) |
| 344 | + self.assertNotIn("author", formset.forms[0].fields) |
| 345 | + |
| 346 | + def test_formset_initial_with_contenttype_instance(self): |
| 347 | + """Test that polymorphic_ctype can be set as ContentType instance in initial data (issue #549)""" |
| 348 | + from django.contrib.contenttypes.models import ContentType |
| 349 | + |
| 350 | + ct = ContentType.objects.get_for_model(SpecialBook, for_concrete_model=False) |
| 351 | + |
| 352 | + SpecialBookFormSet = polymorphic_modelformset_factory( |
| 353 | + Book, |
| 354 | + fields="__all__", |
| 355 | + formset_children=(PolymorphicFormSetChild(SpecialBook, form=SpecialBookForm),), |
| 356 | + ) |
| 357 | + |
| 358 | + # Set initial data with ContentType instance (as users do in issue #549) |
| 359 | + formset = SpecialBookFormSet( |
| 360 | + queryset=SpecialBook.objects.none(), |
| 361 | + initial=[{"polymorphic_ctype": ct}], |
| 362 | + ) |
| 363 | + |
| 364 | + # Should not raise an error when creating the formset |
| 365 | + form = formset.forms[0] |
| 366 | + |
| 367 | + # Verify the polymorphic_ctype field is properly set up with the ID |
| 368 | + self.assertIn("polymorphic_ctype", form.fields) |
| 369 | + |
| 370 | + # The critical assertion: the field's initial value should be the ID (int), |
| 371 | + # not the ContentType instance. This proves the normalization worked. |
| 372 | + self.assertEqual(form.fields["polymorphic_ctype"].initial, ct.pk) |
| 373 | + self.assertIsInstance(form.fields["polymorphic_ctype"].initial, int) |
| 374 | + |
| 375 | + def test_formset_with_none_instance(self): |
| 376 | + """Test that formset handles None instance without AttributeError (issue #363). |
| 377 | +
|
| 378 | + This occurs when a bound formset has a pk that doesn't exist in the queryset, |
| 379 | + causing Django's _existing_object to return None. The polymorphic formset |
| 380 | + must handle this gracefully instead of calling get_real_instance_class() on None. |
| 381 | + """ |
| 382 | + from django.contrib.contenttypes.models import ContentType |
| 383 | + |
| 384 | + ct = ContentType.objects.get_for_model(SpecialBook, for_concrete_model=False) |
| 385 | + |
| 386 | + SpecialBookFormSet = polymorphic_modelformset_factory( |
| 387 | + Book, |
| 388 | + fields="__all__", |
| 389 | + formset_children=(PolymorphicFormSetChild(SpecialBook, form=SpecialBookForm),), |
| 390 | + ) |
| 391 | + |
| 392 | + # Simulate the scenario where _existing_object returns None: |
| 393 | + # - Bound formset with data |
| 394 | + # - Claims to have an initial form (INITIAL_FORMS > 0) |
| 395 | + # - But the pk doesn't exist in queryset, so _existing_object returns None |
| 396 | + data = { |
| 397 | + "form-TOTAL_FORMS": "1", |
| 398 | + "form-INITIAL_FORMS": "1", |
| 399 | + "form-MIN_NUM_FORMS": "0", |
| 400 | + "form-MAX_NUM_FORMS": "1000", |
| 401 | + "form-0-id": "99999", # Non-existent pk - _existing_object will return None |
| 402 | + "form-0-polymorphic_ctype": str(ct.pk), |
| 403 | + } |
| 404 | + |
| 405 | + formset = SpecialBookFormSet(data=data, queryset=SpecialBook.objects.none()) |
| 406 | + |
| 407 | + # This should not raise AttributeError when instance is None |
| 408 | + forms = formset.forms |
| 409 | + self.assertEqual(len(forms), 1) |
| 410 | + self.assertIn("polymorphic_ctype", forms[0].fields) |
| 411 | + |
| 412 | + def test_combined_formset_behaviors(self): |
| 413 | + # 1. __init__ exclude handling |
| 414 | + child_none = PolymorphicFormSetChild(Book, form=SpecialBookForm, exclude=None) |
| 415 | + self.assertEqual(child_none.exclude, ()) |
| 416 | + |
| 417 | + child_list = PolymorphicFormSetChild(Book, form=SpecialBookForm, exclude=["author"]) |
| 418 | + self.assertIn("author", child_list.exclude) |
| 419 | + |
| 420 | + # 2. get_form exclude merging |
| 421 | + form = child_list.get_form(extra_exclude=["field1"]) |
| 422 | + self.assertIn("author", form._meta.exclude) |
| 423 | + self.assertIn("field1", form._meta.exclude) |
| 424 | + |
| 425 | + form_meta_default = child_none.get_form() |
| 426 | + self.assertIn("author", form_meta_default._meta.exclude) |
| 427 | + |
| 428 | + # 3. polymorphic_ctype normalization |
| 429 | + ct = ContentType.objects.get_for_model(SpecialBook, for_concrete_model=False) |
| 430 | + SpecialBookFormSet = polymorphic_modelformset_factory( |
| 431 | + Book, |
| 432 | + fields="__all__", |
| 433 | + formset_children=(PolymorphicFormSetChild(SpecialBook, form=SpecialBookForm),), |
| 434 | + ) |
| 435 | + formset = SpecialBookFormSet( |
| 436 | + queryset=SpecialBook.objects.none(), |
| 437 | + initial=[{"polymorphic_ctype": ct}], |
| 438 | + ) |
| 439 | + # The formset should normalize the ContentType instance to its ID |
| 440 | + form_ct = formset.forms[0] |
| 441 | + self.assertIsInstance(form_ct.initial["polymorphic_ctype"], int) |
| 442 | + self.assertEqual(form_ct.initial["polymorphic_ctype"], ct.pk) |
0 commit comments