| 1 |
""" |
| 2 |
Tests for polymorphic formsets. |
| 3 |
""" |
| 4 |
|
| 5 |
import pytest |
| 6 |
from django import forms |
| 7 |
from django.contrib.contenttypes.models import ContentType |
| 8 |
from django.core.exceptions import ImproperlyConfigured, ValidationError |
| 9 |
from django.test import TestCase |
| 10 |
|
| 11 |
from polymorphic.formsets.generic import ( |
| 12 |
GenericPolymorphicFormSetChild, |
| 13 |
generic_polymorphic_inlineformset_factory, |
| 14 |
) |
| 15 |
from polymorphic.formsets.models import ( |
| 16 |
PolymorphicFormSetChild, |
| 17 |
UnsupportedChildType, |
| 18 |
polymorphic_inlineformset_factory, |
| 19 |
polymorphic_modelformset_factory, |
| 20 |
) |
| 21 |
from polymorphic.tests.models import ( |
| 22 |
GenericFKParent, |
| 23 |
Model2A, |
| 24 |
Model2B, |
| 25 |
Model2C, |
| 26 |
PolymorphicTagA, |
| 27 |
PolymorphicTagB, |
| 28 |
PolymorphicTagBase, |
| 29 |
) |
| 30 |
|
| 31 |
|
| 32 |
class PolymorphicFormSetChildTest(TestCase): |
| 33 |
"""Test PolymorphicFormSetChild configuration""" |
| 34 |
|
| 35 |
def test_content_type_property(self): |
| 36 |
"""ContentType is cached for child model""" |
| 37 |
child = PolymorphicFormSetChild(model=Model2A) |
| 38 |
ct = child.content_type |
| 39 |
|
| 40 |
assert ct.model_class() == Model2A |
| 41 |
assert child.content_type is ct # Verify caching |
| 42 |
|
| 43 |
def test_extra_exclude_parameter(self): |
| 44 |
"""extra_exclude adds to existing exclude list""" |
| 45 |
child = PolymorphicFormSetChild(model=Model2A, exclude=["field1"]) |
| 46 |
form_class = child.get_form(extra_exclude=["field2"]) |
| 47 |
form = form_class() |
| 48 |
|
| 49 |
assert "field1" not in form.fields |
| 50 |
assert "field2" not in form.fields |
| 51 |
|
| 52 |
|
| 53 |
class PolymorphicModelFormSetTest(TestCase): |
| 54 |
"""Test polymorphic model formset functionality""" |
| 55 |
|
| 56 |
def setUp(self): |
| 57 |
self.obj_a = Model2A.objects.create(field1="A1") |
| 58 |
self.obj_b = Model2B.objects.create(field1="B1", field2="B2") |
| 59 |
|
| 60 |
def test_empty_form_property_raises_error(self): |
| 61 |
"""Accessing empty_form raises RuntimeError (use empty_forms instead)""" |
| 62 |
FormSet = polymorphic_modelformset_factory( |
| 63 |
model=Model2A, |
| 64 |
fields="__all__", |
| 65 |
formset_children=[PolymorphicFormSetChild(model=Model2A)], |
| 66 |
) |
| 67 |
formset = FormSet(queryset=Model2A.objects.none()) |
| 68 |
|
| 69 |
with pytest.raises(RuntimeError, match="use 'empty_forms'"): |
| 70 |
_ = formset.empty_form |
| 71 |
|
| 72 |
def test_error_no_child_forms(self): |
| 73 |
"""get_form_class raises ImproperlyConfigured when child_forms empty""" |
| 74 |
FormSet = polymorphic_modelformset_factory( |
| 75 |
model=Model2A, |
| 76 |
fields="__all__", |
| 77 |
formset_children=[PolymorphicFormSetChild(model=Model2A)], |
| 78 |
) |
| 79 |
formset = FormSet(queryset=Model2A.objects.none()) |
| 80 |
formset.child_forms = {} |
| 81 |
|
| 82 |
with pytest.raises(ImproperlyConfigured, match="No 'child_forms' defined"): |
| 83 |
formset.get_form_class(Model2A) |
| 84 |
|
| 85 |
def test_error_non_polymorphic_model(self): |
| 86 |
"""get_form_class raises TypeError for non-polymorphic models""" |
| 87 |
from django.contrib.auth.models import User |
| 88 |
|
| 89 |
FormSet = polymorphic_modelformset_factory( |
| 90 |
model=Model2A, |
| 91 |
fields="__all__", |
| 92 |
formset_children=[PolymorphicFormSetChild(model=Model2A)], |
| 93 |
) |
| 94 |
formset = FormSet(queryset=Model2A.objects.none()) |
| 95 |
|
| 96 |
with pytest.raises(TypeError, match="Expect polymorphic model"): |
| 97 |
formset.get_form_class(User) |
| 98 |
|
| 99 |
def test_error_unsupported_child_type(self): |
| 100 |
"""get_form_class raises UnsupportedChildType for unregistered models""" |
| 101 |
FormSet = polymorphic_modelformset_factory( |
| 102 |
model=Model2A, |
| 103 |
fields="__all__", |
| 104 |
formset_children=[PolymorphicFormSetChild(model=Model2A)], |
| 105 |
) |
| 106 |
formset = FormSet(queryset=Model2A.objects.none()) |
| 107 |
|
| 108 |
with pytest.raises(UnsupportedChildType, match="no form class is registered"): |
| 109 |
formset.get_form_class(Model2B) |
| 110 |
|
| 111 |
def test_bound_formset_with_data(self): |
| 112 |
"""Bound formset processes existing objects correctly""" |
| 113 |
FormSet = polymorphic_modelformset_factory( |
| 114 |
model=Model2A, |
| 115 |
fields="__all__", |
| 116 |
formset_children=[ |
| 117 |
PolymorphicFormSetChild(model=Model2A), |
| 118 |
PolymorphicFormSetChild(model=Model2B), |
| 119 |
], |
| 120 |
) |
| 121 |
|
| 122 |
ct_a = ContentType.objects.get_for_model(Model2A, for_concrete_model=False) |
| 123 |
ct_b = ContentType.objects.get_for_model(Model2B, for_concrete_model=False) |
| 124 |
|
| 125 |
data = { |
| 126 |
"form-TOTAL_FORMS": "2", |
| 127 |
"form-INITIAL_FORMS": "2", |
| 128 |
"form-MIN_NUM_FORMS": "0", |
| 129 |
"form-MAX_NUM_FORMS": "1000", |
| 130 |
"form-0-id": str(self.obj_a.pk), |
| 131 |
"form-0-field1": "Modified A", |
| 132 |
"form-0-polymorphic_ctype": str(ct_a.pk), |
| 133 |
"form-1-id": str(self.obj_b.pk), |
| 134 |
"form-1-field1": "Modified B", |
| 135 |
"form-1-field2": "Modified B2", |
| 136 |
"form-1-polymorphic_ctype": str(ct_b.pk), |
| 137 |
} |
| 138 |
|
| 139 |
queryset = Model2A.objects.filter(pk__in=[self.obj_a.pk, self.obj_b.pk]) |
| 140 |
formset = FormSet(data=data, queryset=queryset) |
| 141 |
|
| 142 |
assert formset.is_bound |
| 143 |
assert formset.is_valid() |
| 144 |
assert formset.forms[0].instance.pk == self.obj_a.pk |
| 145 |
assert formset.forms[1].instance.pk == self.obj_b.pk |
| 146 |
|
| 147 |
def test_extra_forms_cycle_child_types(self): |
| 148 |
"""Extra forms cycle through registered child types""" |
| 149 |
FormSet = polymorphic_modelformset_factory( |
| 150 |
model=Model2A, |
| 151 |
fields="__all__", |
| 152 |
extra=3, |
| 153 |
formset_children=[ |
| 154 |
PolymorphicFormSetChild(model=Model2A), |
| 155 |
PolymorphicFormSetChild(model=Model2B), |
| 156 |
], |
| 157 |
) |
| 158 |
|
| 159 |
formset = FormSet(queryset=Model2A.objects.none()) |
| 160 |
|
| 161 |
# Forms cycle: A, B, A |
| 162 |
assert "field2" not in formset.forms[0].fields # Model2A |
| 163 |
assert "field2" in formset.forms[1].fields # Model2B |
| 164 |
assert "field2" not in formset.forms[2].fields # Model2A |
| 165 |
|
| 166 |
def test_validation_error_missing_ctype(self): |
| 167 |
"""ValidationError raised when polymorphic_ctype missing in bound data""" |
| 168 |
FormSet = polymorphic_modelformset_factory( |
| 169 |
model=Model2A, |
| 170 |
fields="__all__", |
| 171 |
formset_children=[PolymorphicFormSetChild(model=Model2A)], |
| 172 |
) |
| 173 |
|
| 174 |
data = { |
| 175 |
"form-TOTAL_FORMS": "1", |
| 176 |
"form-INITIAL_FORMS": "0", |
| 177 |
"form-MIN_NUM_FORMS": "0", |
| 178 |
"form-MAX_NUM_FORMS": "1000", |
| 179 |
"form-0-field1": "Test", |
| 180 |
} |
| 181 |
|
| 182 |
formset = FormSet(data=data, queryset=Model2A.objects.none()) |
| 183 |
|
| 184 |
with pytest.raises(ValidationError, match="has no 'polymorphic_ctype'"): |
| 185 |
_ = formset.forms |
| 186 |
|
| 187 |
def test_unsupported_child_in_bound_data(self): |
| 188 |
"""UnsupportedChildType when bound data has unregistered child type""" |
| 189 |
FormSet = polymorphic_modelformset_factory( |
| 190 |
model=Model2A, |
| 191 |
fields="__all__", |
| 192 |
formset_children=[PolymorphicFormSetChild(model=Model2A)], |
| 193 |
) |
| 194 |
|
| 195 |
ct_b = ContentType.objects.get_for_model(Model2B, for_concrete_model=False) |
| 196 |
data = { |
| 197 |
"form-TOTAL_FORMS": "1", |
| 198 |
"form-INITIAL_FORMS": "0", |
| 199 |
"form-MIN_NUM_FORMS": "0", |
| 200 |
"form-MAX_NUM_FORMS": "1000", |
| 201 |
"form-0-field1": "Test", |
| 202 |
"form-0-polymorphic_ctype": str(ct_b.pk), |
| 203 |
} |
| 204 |
|
| 205 |
formset = FormSet(data=data, queryset=Model2A.objects.none()) |
| 206 |
|
| 207 |
with pytest.raises(UnsupportedChildType, match="is not part of the formset"): |
| 208 |
_ = formset.forms |
| 209 |
|
| 210 |
def test_unbound_with_ctype_in_initial(self): |
| 211 |
"""Unbound formset with polymorphic_ctype in initial creates correct form""" |
| 212 |
FormSet = polymorphic_modelformset_factory( |
| 213 |
model=Model2A, |
| 214 |
fields="__all__", |
| 215 |
extra=1, |
| 216 |
formset_children=[ |
| 217 |
PolymorphicFormSetChild(model=Model2A), |
| 218 |
PolymorphicFormSetChild(model=Model2B), |
| 219 |
], |
| 220 |
) |
| 221 |
|
| 222 |
ct_b = ContentType.objects.get_for_model(Model2B, for_concrete_model=False) |
| 223 |
initial = [{"polymorphic_ctype": ct_b}] |
| 224 |
formset = FormSet(queryset=Model2A.objects.none(), initial=initial) |
| 225 |
|
| 226 |
# Form should be for Model2B |
| 227 |
assert "field2" in formset.forms[0].fields |
| 228 |
|
| 229 |
def test_child_form_kwargs(self): |
| 230 |
"""child_form_kwargs passed to child form factory""" |
| 231 |
FormSet = polymorphic_modelformset_factory( |
| 232 |
model=Model2A, |
| 233 |
fields="__all__", |
| 234 |
formset_children=[PolymorphicFormSetChild(model=Model2A)], |
| 235 |
child_form_kwargs={"extra_exclude": ["field1"]}, |
| 236 |
) |
| 237 |
|
| 238 |
formset = FormSet(queryset=Model2A.objects.none()) |
| 239 |
assert "field1" not in formset.forms[0].fields |
| 240 |
|
| 241 |
def test_is_multipart_with_file_field(self): |
| 242 |
"""is_multipart returns True when form has FileField""" |
| 243 |
|
| 244 |
class FileForm(forms.ModelForm): |
| 245 |
file = forms.FileField() |
| 246 |
|
| 247 |
class Meta: |
| 248 |
model = Model2A |
| 249 |
fields = ["field1"] |
| 250 |
|
| 251 |
FormSet = polymorphic_modelformset_factory( |
| 252 |
model=Model2A, |
| 253 |
fields=["field1"], |
| 254 |
formset_children=[PolymorphicFormSetChild(model=Model2A, form=FileForm)], |
| 255 |
extra=1, |
| 256 |
) |
| 257 |
formset = FormSet(queryset=Model2A.objects.none()) |
| 258 |
|
| 259 |
assert formset.is_multipart() |
| 260 |
|
| 261 |
def test_media_aggregation(self): |
| 262 |
"""media property aggregates all child form media""" |
| 263 |
|
| 264 |
class MediaForm(forms.ModelForm): |
| 265 |
class Media: |
| 266 |
js = ("test.js",) |
| 267 |
|
| 268 |
class Meta: |
| 269 |
model = Model2A |
| 270 |
fields = ["field1"] |
| 271 |
|
| 272 |
FormSet = polymorphic_modelformset_factory( |
| 273 |
model=Model2A, |
| 274 |
fields=["field1"], |
| 275 |
formset_children=[PolymorphicFormSetChild(model=Model2A, form=MediaForm)], |
| 276 |
extra=1, |
| 277 |
) |
| 278 |
formset = FormSet(queryset=Model2A.objects.none()) |
| 279 |
|
| 280 |
assert "test.js" in str(formset.media) |
| 281 |
|
| 282 |
|
| 283 |
class PolymorphicInlineFormSetTest(TestCase): |
| 284 |
"""Test polymorphic inline formsets""" |
| 285 |
|
| 286 |
def test_inline_formset_factory(self): |
| 287 |
"""Inline formset factory creates functional formsets""" |
| 288 |
InlineFormSet = polymorphic_inlineformset_factory( |
| 289 |
parent_model=Model2A, |
| 290 |
model=Model2B, |
| 291 |
fields="__all__", |
| 292 |
formset_children=[ |
| 293 |
PolymorphicFormSetChild(model=Model2B), |
| 294 |
PolymorphicFormSetChild(model=Model2C), |
| 295 |
], |
| 296 |
) |
| 297 |
|
| 298 |
parent = Model2A.objects.create(field1="Parent") |
| 299 |
formset = InlineFormSet(instance=parent) |
| 300 |
|
| 301 |
assert formset.instance == parent |
| 302 |
assert len(formset.forms) > 0 |
| 303 |
|
| 304 |
def test_inline_with_child_form_kwargs(self): |
| 305 |
"""Inline formset passes child_form_kwargs to children""" |
| 306 |
InlineFormSet = polymorphic_inlineformset_factory( |
| 307 |
parent_model=Model2A, |
| 308 |
model=Model2B, |
| 309 |
fields="__all__", |
| 310 |
formset_children=[PolymorphicFormSetChild(model=Model2B)], |
| 311 |
child_form_kwargs={"extra_exclude": ["field1"]}, |
| 312 |
) |
| 313 |
|
| 314 |
parent = Model2A.objects.create(field1="Parent") |
| 315 |
formset = InlineFormSet(instance=parent) |
| 316 |
|
| 317 |
assert "field1" not in formset.forms[0].fields |
| 318 |
|
| 319 |
|
| 320 |
class GenericPolymorphicFormSetChildTest(TestCase): |
| 321 |
"""Test GenericPolymorphicFormSetChild configuration""" |
| 322 |
|
| 323 |
def test_content_type_property(self): |
| 324 |
"""ContentType is cached for generic child model""" |
| 325 |
child = GenericPolymorphicFormSetChild(model=PolymorphicTagA) |
| 326 |
ct = child.content_type |
| 327 |
|
| 328 |
assert ct.model_class() == PolymorphicTagA |
| 329 |
assert child.content_type is ct # Verify caching |
| 330 |
|
| 331 |
def test_ct_field_fk_field_defaults(self): |
| 332 |
"""Default ct_field and fk_field are 'content_type' and 'object_id'""" |
| 333 |
child = GenericPolymorphicFormSetChild(model=PolymorphicTagA) |
| 334 |
assert child.ct_field == "content_type" |
| 335 |
assert child.fk_field == "object_id" |
| 336 |
|
| 337 |
def test_custom_ct_field_fk_field(self): |
| 338 |
"""Custom ct_field and fk_field can be specified""" |
| 339 |
child = GenericPolymorphicFormSetChild( |
| 340 |
model=PolymorphicTagA, ct_field="content_type", fk_field="object_id" |
| 341 |
) |
| 342 |
assert child.ct_field == "content_type" |
| 343 |
assert child.fk_field == "object_id" |
| 344 |
|
| 345 |
def test_get_form_excludes_gfk_fields(self): |
| 346 |
"""get_form automatically excludes the GFK ct_field and fk_field""" |
| 347 |
child = GenericPolymorphicFormSetChild(model=PolymorphicTagA) |
| 348 |
form_class = child.get_form() |
| 349 |
form = form_class() |
| 350 |
|
| 351 |
# The GFK fields should be excluded |
| 352 |
assert "content_type" not in form.fields |
| 353 |
assert "object_id" not in form.fields |
| 354 |
# But model-specific fields should be present |
| 355 |
assert "tag" in form.fields |
| 356 |
assert "priority" in form.fields |
| 357 |
|
| 358 |
def test_get_form_with_extra_exclude(self): |
| 359 |
"""get_form with extra_exclude adds to existing excludes""" |
| 360 |
child = GenericPolymorphicFormSetChild(model=PolymorphicTagA, exclude=["tag"]) |
| 361 |
form_class = child.get_form(extra_exclude=["priority"]) |
| 362 |
form = form_class() |
| 363 |
|
| 364 |
assert "tag" not in form.fields |
| 365 |
assert "priority" not in form.fields |
| 366 |
# GFK fields also excluded |
| 367 |
assert "content_type" not in form.fields |
| 368 |
assert "object_id" not in form.fields |
| 369 |
|
| 370 |
def test_get_form_invalid_ct_field_raises(self): |
| 371 |
"""get_form raises Exception if ct_field is not a ForeignKey to ContentType""" |
| 372 |
child = GenericPolymorphicFormSetChild( |
| 373 |
model=PolymorphicTagA, ct_field="tag", fk_field="object_id" |
| 374 |
) |
| 375 |
|
| 376 |
with pytest.raises(Exception, match="is not a ForeignKey to ContentType"): |
| 377 |
child.get_form() |
| 378 |
|
| 379 |
|
| 380 |
class GenericPolymorphicInlineFormSetTest(TestCase): |
| 381 |
"""Test generic polymorphic inline formsets""" |
| 382 |
|
| 383 |
def setUp(self): |
| 384 |
self.parent = GenericFKParent.objects.create(name="Test Parent") |
| 385 |
self.parent_ct = ContentType.objects.get_for_model(GenericFKParent) |
| 386 |
|
| 387 |
def test_factory_creates_functional_formset(self): |
| 388 |
"""generic_polymorphic_inlineformset_factory creates functional formsets""" |
| 389 |
FormSet = generic_polymorphic_inlineformset_factory( |
| 390 |
model=PolymorphicTagBase, |
| 391 |
formset_children=[ |
| 392 |
GenericPolymorphicFormSetChild(model=PolymorphicTagA), |
| 393 |
GenericPolymorphicFormSetChild(model=PolymorphicTagB), |
| 394 |
], |
| 395 |
) |
| 396 |
|
| 397 |
formset = FormSet(instance=self.parent) |
| 398 |
|
| 399 |
assert formset.instance == self.parent |
| 400 |
assert len(formset.forms) > 0 |
| 401 |
|
| 402 |
def test_formset_gfk_fields_excluded(self): |
| 403 |
"""GFK fields are excluded from forms in the formset""" |
| 404 |
FormSet = generic_polymorphic_inlineformset_factory( |
| 405 |
model=PolymorphicTagBase, |
| 406 |
formset_children=[ |
| 407 |
GenericPolymorphicFormSetChild(model=PolymorphicTagA), |
| 408 |
], |
| 409 |
) |
| 410 |
|
| 411 |
formset = FormSet(instance=self.parent) |
| 412 |
|
| 413 |
# Check all forms have GFK fields excluded |
| 414 |
for form in formset.forms: |
| 415 |
assert "content_type" not in form.fields |
| 416 |
assert "object_id" not in form.fields |
| 417 |
|
| 418 |
def test_extra_forms_cycle_child_types(self): |
| 419 |
"""Extra forms cycle through registered child types""" |
| 420 |
FormSet = generic_polymorphic_inlineformset_factory( |
| 421 |
model=PolymorphicTagBase, |
| 422 |
extra=3, |
| 423 |
formset_children=[ |
| 424 |
GenericPolymorphicFormSetChild(model=PolymorphicTagA), |
| 425 |
GenericPolymorphicFormSetChild(model=PolymorphicTagB), |
| 426 |
], |
| 427 |
) |
| 428 |
|
| 429 |
formset = FormSet(instance=self.parent) |
| 430 |
|
| 431 |
# Forms cycle: A, B, A |
| 432 |
assert "priority" in formset.forms[0].fields # PolymorphicTagA |
| 433 |
assert "color" in formset.forms[1].fields # PolymorphicTagB |
| 434 |
assert "priority" in formset.forms[2].fields # PolymorphicTagA |
| 435 |
|
| 436 |
def test_bound_formset_with_existing_objects(self): |
| 437 |
"""Bound formset processes existing polymorphic objects correctly""" |
| 438 |
tag_a = PolymorphicTagA.objects.create( |
| 439 |
tag="tag-a", |
| 440 |
content_type=self.parent_ct, |
| 441 |
object_id=self.parent.pk, |
| 442 |
priority=5, |
| 443 |
) |
| 444 |
tag_b = PolymorphicTagB.objects.create( |
| 445 |
tag="tag-b", |
| 446 |
content_type=self.parent_ct, |
| 447 |
object_id=self.parent.pk, |
| 448 |
color="red", |
| 449 |
) |
| 450 |
|
| 451 |
FormSet = generic_polymorphic_inlineformset_factory( |
| 452 |
model=PolymorphicTagBase, |
| 453 |
formset_children=[ |
| 454 |
GenericPolymorphicFormSetChild(model=PolymorphicTagA), |
| 455 |
GenericPolymorphicFormSetChild(model=PolymorphicTagB), |
| 456 |
], |
| 457 |
) |
| 458 |
|
| 459 |
ct_a = ContentType.objects.get_for_model(PolymorphicTagA, for_concrete_model=False) |
| 460 |
ct_b = ContentType.objects.get_for_model(PolymorphicTagB, for_concrete_model=False) |
| 461 |
|
| 462 |
data = { |
| 463 |
"tests-polymorphictagbase-content_type-object_id-TOTAL_FORMS": "2", |
| 464 |
"tests-polymorphictagbase-content_type-object_id-INITIAL_FORMS": "2", |
| 465 |
"tests-polymorphictagbase-content_type-object_id-MIN_NUM_FORMS": "0", |
| 466 |
"tests-polymorphictagbase-content_type-object_id-MAX_NUM_FORMS": "1000", |
| 467 |
"tests-polymorphictagbase-content_type-object_id-0-id": str(tag_a.pk), |
| 468 |
"tests-polymorphictagbase-content_type-object_id-0-tag": "tag-a-modified", |
| 469 |
"tests-polymorphictagbase-content_type-object_id-0-priority": "10", |
| 470 |
"tests-polymorphictagbase-content_type-object_id-0-polymorphic_ctype": str(ct_a.pk), |
| 471 |
"tests-polymorphictagbase-content_type-object_id-1-id": str(tag_b.pk), |
| 472 |
"tests-polymorphictagbase-content_type-object_id-1-tag": "tag-b-modified", |
| 473 |
"tests-polymorphictagbase-content_type-object_id-1-color": "blue", |
| 474 |
"tests-polymorphictagbase-content_type-object_id-1-polymorphic_ctype": str(ct_b.pk), |
| 475 |
} |
| 476 |
|
| 477 |
formset = FormSet(data=data, instance=self.parent) |
| 478 |
|
| 479 |
assert formset.is_bound |
| 480 |
assert formset.is_valid(), formset.errors |
| 481 |
assert formset.forms[0].instance.pk == tag_a.pk |
| 482 |
assert formset.forms[1].instance.pk == tag_b.pk |
| 483 |
|
| 484 |
def test_formset_with_child_form_kwargs(self): |
| 485 |
"""child_form_kwargs passed to child form factory""" |
| 486 |
FormSet = generic_polymorphic_inlineformset_factory( |
| 487 |
model=PolymorphicTagBase, |
| 488 |
formset_children=[ |
| 489 |
GenericPolymorphicFormSetChild(model=PolymorphicTagA), |
| 490 |
], |
| 491 |
child_form_kwargs={"extra_exclude": ["tag"]}, |
| 492 |
) |
| 493 |
|
| 494 |
formset = FormSet(instance=self.parent) |
| 495 |
assert "tag" not in formset.forms[0].fields |
| 496 |
|
| 497 |
def test_empty_forms_property(self): |
| 498 |
"""empty_forms returns all possible empty form types""" |
| 499 |
FormSet = generic_polymorphic_inlineformset_factory( |
| 500 |
model=PolymorphicTagBase, |
| 501 |
formset_children=[ |
| 502 |
GenericPolymorphicFormSetChild(model=PolymorphicTagA), |
| 503 |
GenericPolymorphicFormSetChild(model=PolymorphicTagB), |
| 504 |
], |
| 505 |
) |
| 506 |
|
| 507 |
formset = FormSet(instance=self.parent) |
| 508 |
empty_forms = formset.empty_forms |
| 509 |
|
| 510 |
assert len(empty_forms) == 2 |
| 511 |
# Check that different form types are represented |
| 512 |
form_models = {form._meta.model for form in empty_forms} |
| 513 |
assert PolymorphicTagA in form_models |
| 514 |
assert PolymorphicTagB in form_models |
| 515 |
|
| 516 |
def test_empty_form_raises_runtime_error(self): |
| 517 |
"""Accessing empty_form raises RuntimeError (use empty_forms instead)""" |
| 518 |
FormSet = generic_polymorphic_inlineformset_factory( |
| 519 |
model=PolymorphicTagBase, |
| 520 |
formset_children=[ |
| 521 |
GenericPolymorphicFormSetChild(model=PolymorphicTagA), |
| 522 |
], |
| 523 |
) |
| 524 |
|
| 525 |
formset = FormSet(instance=self.parent) |
| 526 |
|
| 527 |
with pytest.raises(RuntimeError, match="use 'empty_forms'"): |
| 528 |
_ = formset.empty_form |
| 529 |
|
| 530 |
def test_unsupported_child_type_in_bound_data(self): |
| 531 |
"""UnsupportedChildType when bound data has unregistered child type""" |
| 532 |
FormSet = generic_polymorphic_inlineformset_factory( |
| 533 |
model=PolymorphicTagBase, |
| 534 |
formset_children=[ |
| 535 |
GenericPolymorphicFormSetChild(model=PolymorphicTagA), |
| 536 |
], |
| 537 |
) |
| 538 |
|
| 539 |
ct_b = ContentType.objects.get_for_model(PolymorphicTagB, for_concrete_model=False) |
| 540 |
data = { |
| 541 |
"tests-polymorphictagbase-content_type-object_id-TOTAL_FORMS": "1", |
| 542 |
"tests-polymorphictagbase-content_type-object_id-INITIAL_FORMS": "0", |
| 543 |
"tests-polymorphictagbase-content_type-object_id-MIN_NUM_FORMS": "0", |
| 544 |
"tests-polymorphictagbase-content_type-object_id-MAX_NUM_FORMS": "1000", |
| 545 |
"tests-polymorphictagbase-content_type-object_id-0-tag": "test", |
| 546 |
"tests-polymorphictagbase-content_type-object_id-0-polymorphic_ctype": str(ct_b.pk), |
| 547 |
} |
| 548 |
|
| 549 |
formset = FormSet(data=data, instance=self.parent) |
| 550 |
|
| 551 |
with pytest.raises(UnsupportedChildType, match="is not part of the formset"): |
| 552 |
_ = formset.forms |
| 553 |
|
| 554 |
def test_validation_error_missing_ctype(self): |
| 555 |
"""ValidationError raised when polymorphic_ctype missing in bound data""" |
| 556 |
FormSet = generic_polymorphic_inlineformset_factory( |
| 557 |
model=PolymorphicTagBase, |
| 558 |
formset_children=[ |
| 559 |
GenericPolymorphicFormSetChild(model=PolymorphicTagA), |
| 560 |
], |
| 561 |
) |
| 562 |
|
| 563 |
data = { |
| 564 |
"tests-polymorphictagbase-content_type-object_id-TOTAL_FORMS": "1", |
| 565 |
"tests-polymorphictagbase-content_type-object_id-INITIAL_FORMS": "0", |
| 566 |
"tests-polymorphictagbase-content_type-object_id-MIN_NUM_FORMS": "0", |
| 567 |
"tests-polymorphictagbase-content_type-object_id-MAX_NUM_FORMS": "1000", |
| 568 |
"tests-polymorphictagbase-content_type-object_id-0-tag": "test", |
| 569 |
} |
| 570 |
|
| 571 |
formset = FormSet(data=data, instance=self.parent) |
| 572 |
|
| 573 |
with pytest.raises(ValidationError, match="has no 'polymorphic_ctype'"): |
| 574 |
_ = formset.forms |
| 575 |
|
| 576 |
def test_is_multipart_with_file_field(self): |
| 577 |
"""is_multipart returns True when form has FileField""" |
| 578 |
|
| 579 |
class FileForm(forms.ModelForm): |
| 580 |
file = forms.FileField() |
| 581 |
|
| 582 |
class Meta: |
| 583 |
model = PolymorphicTagA |
| 584 |
fields = ["tag", "priority"] |
| 585 |
|
| 586 |
FormSet = generic_polymorphic_inlineformset_factory( |
| 587 |
model=PolymorphicTagBase, |
| 588 |
formset_children=[ |
| 589 |
GenericPolymorphicFormSetChild(model=PolymorphicTagA, form=FileForm), |
| 590 |
], |
| 591 |
extra=1, |
| 592 |
) |
| 593 |
|
| 594 |
formset = FormSet(instance=self.parent) |
| 595 |
assert formset.is_multipart() |
| 596 |
|
| 597 |
def test_media_aggregation(self): |
| 598 |
"""media property aggregates all child form media""" |
| 599 |
|
| 600 |
class MediaForm(forms.ModelForm): |
| 601 |
class Media: |
| 602 |
js = ("generic_test.js",) |
| 603 |
|
| 604 |
class Meta: |
| 605 |
model = PolymorphicTagA |
| 606 |
fields = ["tag", "priority"] |
| 607 |
|
| 608 |
FormSet = generic_polymorphic_inlineformset_factory( |
| 609 |
model=PolymorphicTagBase, |
| 610 |
formset_children=[ |
| 611 |
GenericPolymorphicFormSetChild(model=PolymorphicTagA, form=MediaForm), |
| 612 |
], |
| 613 |
extra=1, |
| 614 |
) |
| 615 |
|
| 616 |
formset = FormSet(instance=self.parent) |
| 617 |
assert "generic_test.js" in str(formset.media) |
| 618 |
|
| 619 |
def test_save_new_objects(self): |
| 620 |
"""Formset can save new polymorphic objects via generic relation""" |
| 621 |
FormSet = generic_polymorphic_inlineformset_factory( |
| 622 |
model=PolymorphicTagBase, |
| 623 |
formset_children=[ |
| 624 |
GenericPolymorphicFormSetChild(model=PolymorphicTagA), |
| 625 |
GenericPolymorphicFormSetChild(model=PolymorphicTagB), |
| 626 |
], |
| 627 |
) |
| 628 |
|
| 629 |
ct_a = ContentType.objects.get_for_model(PolymorphicTagA, for_concrete_model=False) |
| 630 |
|
| 631 |
data = { |
| 632 |
"tests-polymorphictagbase-content_type-object_id-TOTAL_FORMS": "1", |
| 633 |
"tests-polymorphictagbase-content_type-object_id-INITIAL_FORMS": "0", |
| 634 |
"tests-polymorphictagbase-content_type-object_id-MIN_NUM_FORMS": "0", |
| 635 |
"tests-polymorphictagbase-content_type-object_id-MAX_NUM_FORMS": "1000", |
| 636 |
"tests-polymorphictagbase-content_type-object_id-0-tag": "new-tag", |
| 637 |
"tests-polymorphictagbase-content_type-object_id-0-priority": "99", |
| 638 |
"tests-polymorphictagbase-content_type-object_id-0-polymorphic_ctype": str(ct_a.pk), |
| 639 |
} |
| 640 |
|
| 641 |
formset = FormSet(data=data, instance=self.parent) |
| 642 |
assert formset.is_valid(), formset.errors |
| 643 |
|
| 644 |
saved_objects = formset.save() |
| 645 |
assert len(saved_objects) == 1 |
| 646 |
assert isinstance(saved_objects[0], PolymorphicTagA) |
| 647 |
assert saved_objects[0].tag == "new-tag" |
| 648 |
assert saved_objects[0].priority == 99 |
| 649 |
assert saved_objects[0].content_object == self.parent |