| 1 |
from django import forms |
| 2 |
from django.contrib.contenttypes.forms import generic_inlineformset_factory |
| 3 |
from django.contrib.contenttypes.models import ContentType |
| 4 |
from django.db import models |
| 5 |
from django.test import TestCase |
| 6 |
from django.test.utils import isolate_apps |
| 7 |
|
| 8 |
from .models import ( |
| 9 |
Animal, |
| 10 |
ForProxyModelModel, |
| 11 |
Gecko, |
| 12 |
Mineral, |
| 13 |
ProxyRelatedModel, |
| 14 |
TaggedItem, |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
class CustomWidget(forms.TextInput): |
| 19 |
pass |
| 20 |
|
| 21 |
|
| 22 |
class TaggedItemForm(forms.ModelForm): |
| 23 |
class Meta: |
| 24 |
model = TaggedItem |
| 25 |
fields = "__all__" |
| 26 |
widgets = {"tag": CustomWidget} |
| 27 |
|
| 28 |
|
| 29 |
class GenericInlineFormsetTests(TestCase): |
| 30 |
def test_output(self): |
| 31 |
GenericFormSet = generic_inlineformset_factory(TaggedItem, extra=1) |
| 32 |
formset = GenericFormSet() |
| 33 |
self.assertHTMLEqual( |
| 34 |
"".join(form.as_p() for form in formset.forms), |
| 35 |
""" |
| 36 |
<p><label |
| 37 |
for="id_generic_relations-taggeditem-content_type-object_id-0-tag"> |
| 38 |
Tag:</label> |
| 39 |
<input id="id_generic_relations-taggeditem-content_type-object_id-0-tag" |
| 40 |
type="text" |
| 41 |
name="generic_relations-taggeditem-content_type-object_id-0-tag" |
| 42 |
maxlength="50"></p> |
| 43 |
<p><label |
| 44 |
for="id_generic_relations-taggeditem-content_type-object_id-0-DELETE"> |
| 45 |
Delete:</label> |
| 46 |
<input type="checkbox" |
| 47 |
name="generic_relations-taggeditem-content_type-object_id-0-DELETE" |
| 48 |
id="id_generic_relations-taggeditem-content_type-object_id-0-DELETE"> |
| 49 |
<input type="hidden" |
| 50 |
name="generic_relations-taggeditem-content_type-object_id-0-id" |
| 51 |
id="id_generic_relations-taggeditem-content_type-object_id-0-id"></p> |
| 52 |
""", |
| 53 |
) |
| 54 |
formset = GenericFormSet(instance=Animal()) |
| 55 |
self.assertHTMLEqual( |
| 56 |
"".join(form.as_p() for form in formset.forms), |
| 57 |
""" |
| 58 |
<p><label |
| 59 |
for="id_generic_relations-taggeditem-content_type-object_id-0-tag"> |
| 60 |
Tag:</label> |
| 61 |
<input id="id_generic_relations-taggeditem-content_type-object_id-0-tag" |
| 62 |
type="text" |
| 63 |
name="generic_relations-taggeditem-content_type-object_id-0-tag" |
| 64 |
maxlength="50"></p> |
| 65 |
<p><label |
| 66 |
for="id_generic_relations-taggeditem-content_type-object_id-0-DELETE"> |
| 67 |
Delete:</label> |
| 68 |
<input type="checkbox" |
| 69 |
name="generic_relations-taggeditem-content_type-object_id-0-DELETE" |
| 70 |
id="id_generic_relations-taggeditem-content_type-object_id-0-DELETE"> |
| 71 |
<input type="hidden" |
| 72 |
name="generic_relations-taggeditem-content_type-object_id-0-id" |
| 73 |
id="id_generic_relations-taggeditem-content_type-object_id-0-id"></p> |
| 74 |
""", |
| 75 |
) |
| 76 |
platypus = Animal.objects.create( |
| 77 |
common_name="Platypus", |
| 78 |
latin_name="Ornithorhynchus anatinus", |
| 79 |
) |
| 80 |
platypus.tags.create(tag="shiny") |
| 81 |
GenericFormSet = generic_inlineformset_factory(TaggedItem, extra=1) |
| 82 |
formset = GenericFormSet(instance=platypus) |
| 83 |
tagged_item_id = TaggedItem.objects.get(tag="shiny", object_id=platypus.id).id |
| 84 |
self.assertHTMLEqual( |
| 85 |
"".join(form.as_p() for form in formset.forms), |
| 86 |
""" |
| 87 |
<p><label |
| 88 |
for="id_generic_relations-taggeditem-content_type-object_id-0-tag"> |
| 89 |
Tag:</label> |
| 90 |
<input id="id_generic_relations-taggeditem-content_type-object_id-0-tag" |
| 91 |
type="text" |
| 92 |
name="generic_relations-taggeditem-content_type-object_id-0-tag" |
| 93 |
value="shiny" maxlength="50"></p> |
| 94 |
<p><label |
| 95 |
for="id_generic_relations-taggeditem-content_type-object_id-0-DELETE"> |
| 96 |
Delete:</label> |
| 97 |
<input type="checkbox" |
| 98 |
name="generic_relations-taggeditem-content_type-object_id-0-DELETE" |
| 99 |
id="id_generic_relations-taggeditem-content_type-object_id-0-DELETE"> |
| 100 |
<input type="hidden" |
| 101 |
name="generic_relations-taggeditem-content_type-object_id-0-id" |
| 102 |
value="%s" |
| 103 |
id="id_generic_relations-taggeditem-content_type-object_id-0-id"></p> |
| 104 |
<p><label |
| 105 |
for="id_generic_relations-taggeditem-content_type-object_id-1-tag"> |
| 106 |
Tag:</label> |
| 107 |
<input id="id_generic_relations-taggeditem-content_type-object_id-1-tag" |
| 108 |
type="text" |
| 109 |
name="generic_relations-taggeditem-content_type-object_id-1-tag" |
| 110 |
maxlength="50"></p> |
| 111 |
<p><label |
| 112 |
for="id_generic_relations-taggeditem-content_type-object_id-1-DELETE"> |
| 113 |
Delete:</label> |
| 114 |
<input type="checkbox" |
| 115 |
name="generic_relations-taggeditem-content_type-object_id-1-DELETE" |
| 116 |
id="id_generic_relations-taggeditem-content_type-object_id-1-DELETE"> |
| 117 |
<input type="hidden" |
| 118 |
name="generic_relations-taggeditem-content_type-object_id-1-id" |
| 119 |
id="id_generic_relations-taggeditem-content_type-object_id-1-id"></p> |
| 120 |
""" % tagged_item_id, |
| 121 |
) |
| 122 |
lion = Animal.objects.create(common_name="Lion", latin_name="Panthera leo") |
| 123 |
formset = GenericFormSet(instance=lion, prefix="x") |
| 124 |
self.assertHTMLEqual( |
| 125 |
"".join(form.as_p() for form in formset.forms), |
| 126 |
""" |
| 127 |
<p><label for="id_x-0-tag">Tag:</label> |
| 128 |
<input id="id_x-0-tag" type="text" name="x-0-tag" maxlength="50"></p> |
| 129 |
<p><label for="id_x-0-DELETE">Delete:</label> |
| 130 |
<input type="checkbox" name="x-0-DELETE" id="id_x-0-DELETE"> |
| 131 |
<input type="hidden" name="x-0-id" id="id_x-0-id"></p> |
| 132 |
""", |
| 133 |
) |
| 134 |
|
| 135 |
def test_options(self): |
| 136 |
TaggedItemFormSet = generic_inlineformset_factory( |
| 137 |
TaggedItem, |
| 138 |
can_delete=False, |
| 139 |
exclude=["tag"], |
| 140 |
extra=3, |
| 141 |
) |
| 142 |
platypus = Animal.objects.create( |
| 143 |
common_name="Platypus", latin_name="Ornithorhynchus anatinus" |
| 144 |
) |
| 145 |
harmless = platypus.tags.create(tag="harmless") |
| 146 |
mammal = platypus.tags.create(tag="mammal") |
| 147 |
# Works without a queryset. |
| 148 |
formset = TaggedItemFormSet(instance=platypus) |
| 149 |
self.assertEqual(len(formset.forms), 5) |
| 150 |
self.assertHTMLEqual( |
| 151 |
formset.forms[0].as_p(), |
| 152 |
'<input type="hidden" ' |
| 153 |
'name="generic_relations-taggeditem-content_type-object_id-0-id" ' |
| 154 |
'value="%s" ' |
| 155 |
'id="id_generic_relations-taggeditem-content_type-object_id-0-id">' |
| 156 |
% harmless.pk, |
| 157 |
) |
| 158 |
self.assertEqual(formset.forms[0].instance, harmless) |
| 159 |
self.assertEqual(formset.forms[1].instance, mammal) |
| 160 |
self.assertIsNone(formset.forms[2].instance.pk) |
| 161 |
# A queryset can be used to alter display ordering. |
| 162 |
formset = TaggedItemFormSet( |
| 163 |
instance=platypus, queryset=TaggedItem.objects.order_by("-tag") |
| 164 |
) |
| 165 |
self.assertEqual(len(formset.forms), 5) |
| 166 |
self.assertEqual(formset.forms[0].instance, mammal) |
| 167 |
self.assertEqual(formset.forms[1].instance, harmless) |
| 168 |
self.assertIsNone(formset.forms[2].instance.pk) |
| 169 |
# A queryset that omits items. |
| 170 |
formset = TaggedItemFormSet( |
| 171 |
instance=platypus, |
| 172 |
queryset=TaggedItem.objects.filter(tag__startswith="harm"), |
| 173 |
) |
| 174 |
self.assertEqual(len(formset.forms), 4) |
| 175 |
self.assertEqual(formset.forms[0].instance, harmless) |
| 176 |
self.assertIsNone(formset.forms[1].instance.pk) |
| 177 |
|
| 178 |
def test_get_queryset_ordering(self): |
| 179 |
""" |
| 180 |
BaseGenericInlineFormSet.get_queryset() adds default ordering, if |
| 181 |
needed. |
| 182 |
""" |
| 183 |
inline_formset = generic_inlineformset_factory(TaggedItem, exclude=("tag",)) |
| 184 |
formset = inline_formset(instance=Gecko.objects.create()) |
| 185 |
self.assertIs(formset.get_queryset().ordered, True) |
| 186 |
|
| 187 |
def test_initial(self): |
| 188 |
quartz = Mineral.objects.create(name="Quartz", hardness=7) |
| 189 |
GenericFormSet = generic_inlineformset_factory(TaggedItem, extra=1) |
| 190 |
ctype = ContentType.objects.get_for_model(quartz) |
| 191 |
initial_data = [ |
| 192 |
{ |
| 193 |
"tag": "lizard", |
| 194 |
"content_type": ctype.pk, |
| 195 |
"object_id": quartz.pk, |
| 196 |
} |
| 197 |
] |
| 198 |
formset = GenericFormSet(initial=initial_data) |
| 199 |
self.assertEqual(formset.forms[0].initial, initial_data[0]) |
| 200 |
|
| 201 |
def test_meta_widgets(self): |
| 202 |
"""TaggedItemForm has a widget defined in Meta.""" |
| 203 |
Formset = generic_inlineformset_factory(TaggedItem, TaggedItemForm) |
| 204 |
form = Formset().forms[0] |
| 205 |
self.assertIsInstance(form["tag"].field.widget, CustomWidget) |
| 206 |
|
| 207 |
@isolate_apps("generic_relations") |
| 208 |
def test_incorrect_content_type(self): |
| 209 |
class BadModel(models.Model): |
| 210 |
content_type = models.PositiveIntegerField() |
| 211 |
|
| 212 |
msg = ( |
| 213 |
"fk_name 'generic_relations.BadModel.content_type' is not a ForeignKey to " |
| 214 |
"ContentType" |
| 215 |
) |
| 216 |
with self.assertRaisesMessage(Exception, msg): |
| 217 |
generic_inlineformset_factory(BadModel, TaggedItemForm) |
| 218 |
|
| 219 |
def test_save_new_uses_form_save(self): |
| 220 |
class SaveTestForm(forms.ModelForm): |
| 221 |
def save(self, *args, **kwargs): |
| 222 |
self.instance.saved_by = "custom method" |
| 223 |
return super().save(*args, **kwargs) |
| 224 |
|
| 225 |
Formset = generic_inlineformset_factory( |
| 226 |
ForProxyModelModel, fields="__all__", form=SaveTestForm |
| 227 |
) |
| 228 |
instance = ProxyRelatedModel.objects.create() |
| 229 |
data = { |
| 230 |
"form-TOTAL_FORMS": "1", |
| 231 |
"form-INITIAL_FORMS": "0", |
| 232 |
"form-MAX_NUM_FORMS": "", |
| 233 |
"form-0-title": "foo", |
| 234 |
} |
| 235 |
formset = Formset(data, instance=instance, prefix="form") |
| 236 |
self.assertTrue(formset.is_valid()) |
| 237 |
new_obj = formset.save()[0] |
| 238 |
self.assertEqual(new_obj.saved_by, "custom method") |
| 239 |
|
| 240 |
def test_save_new_for_proxy(self): |
| 241 |
Formset = generic_inlineformset_factory( |
| 242 |
ForProxyModelModel, fields="__all__", for_concrete_model=False |
| 243 |
) |
| 244 |
instance = ProxyRelatedModel.objects.create() |
| 245 |
data = { |
| 246 |
"form-TOTAL_FORMS": "1", |
| 247 |
"form-INITIAL_FORMS": "0", |
| 248 |
"form-MAX_NUM_FORMS": "", |
| 249 |
"form-0-title": "foo", |
| 250 |
} |
| 251 |
formset = Formset(data, instance=instance, prefix="form") |
| 252 |
self.assertTrue(formset.is_valid()) |
| 253 |
(new_obj,) = formset.save() |
| 254 |
self.assertEqual(new_obj.obj, instance) |
| 255 |
|
| 256 |
def test_save_new_for_concrete(self): |
| 257 |
Formset = generic_inlineformset_factory( |
| 258 |
ForProxyModelModel, fields="__all__", for_concrete_model=True |
| 259 |
) |
| 260 |
instance = ProxyRelatedModel.objects.create() |
| 261 |
data = { |
| 262 |
"form-TOTAL_FORMS": "1", |
| 263 |
"form-INITIAL_FORMS": "0", |
| 264 |
"form-MAX_NUM_FORMS": "", |
| 265 |
"form-0-title": "foo", |
| 266 |
} |
| 267 |
formset = Formset(data, instance=instance, prefix="form") |
| 268 |
self.assertTrue(formset.is_valid()) |
| 269 |
(new_obj,) = formset.save() |
| 270 |
self.assertNotIsInstance(new_obj.obj, ProxyRelatedModel) |
| 271 |
|
| 272 |
def test_initial_count(self): |
| 273 |
GenericFormSet = generic_inlineformset_factory(TaggedItem) |
| 274 |
data = { |
| 275 |
"form-TOTAL_FORMS": "3", |
| 276 |
"form-INITIAL_FORMS": "3", |
| 277 |
"form-MAX_NUM_FORMS": "", |
| 278 |
} |
| 279 |
formset = GenericFormSet(data=data, prefix="form") |
| 280 |
self.assertEqual(formset.initial_form_count(), 3) |
| 281 |
formset = GenericFormSet(data=data, prefix="form", save_as_new=True) |
| 282 |
self.assertEqual(formset.initial_form_count(), 0) |
| 283 |
|
| 284 |
def test_save_as_new(self): |
| 285 |
""" |
| 286 |
The save_as_new parameter creates new items that are associated with |
| 287 |
the object. |
| 288 |
""" |
| 289 |
lion = Animal.objects.create(common_name="Lion", latin_name="Panthera leo") |
| 290 |
yellow = lion.tags.create(tag="yellow") |
| 291 |
hairy = lion.tags.create(tag="hairy") |
| 292 |
GenericFormSet = generic_inlineformset_factory(TaggedItem) |
| 293 |
data = { |
| 294 |
"form-TOTAL_FORMS": "3", |
| 295 |
"form-INITIAL_FORMS": "2", |
| 296 |
"form-MAX_NUM_FORMS": "", |
| 297 |
"form-0-id": str(yellow.pk), |
| 298 |
"form-0-tag": "hunts", |
| 299 |
"form-1-id": str(hairy.pk), |
| 300 |
"form-1-tag": "roars", |
| 301 |
} |
| 302 |
formset = GenericFormSet(data, instance=lion, prefix="form", save_as_new=True) |
| 303 |
self.assertTrue(formset.is_valid()) |
| 304 |
tags = formset.save() |
| 305 |
self.assertEqual([tag.tag for tag in tags], ["hunts", "roars"]) |
| 306 |
hunts, roars = tags |
| 307 |
self.assertSequenceEqual( |
| 308 |
lion.tags.order_by("tag"), [hairy, hunts, roars, yellow] |
| 309 |
) |
| 310 |
|
| 311 |
def test_absolute_max(self): |
| 312 |
GenericFormSet = generic_inlineformset_factory(TaggedItem, absolute_max=1500) |
| 313 |
data = { |
| 314 |
"form-TOTAL_FORMS": "1501", |
| 315 |
"form-INITIAL_FORMS": "0", |
| 316 |
"form-MAX_NUM_FORMS": "0", |
| 317 |
} |
| 318 |
formset = GenericFormSet(data=data, prefix="form") |
| 319 |
self.assertIs(formset.is_valid(), False) |
| 320 |
self.assertEqual(len(formset.forms), 1500) |
| 321 |
self.assertEqual( |
| 322 |
formset.non_form_errors(), |
| 323 |
["Please submit at most 1000 forms."], |
| 324 |
) |
| 325 |
|
| 326 |
def test_absolute_max_with_max_num(self): |
| 327 |
GenericFormSet = generic_inlineformset_factory( |
| 328 |
TaggedItem, |
| 329 |
max_num=20, |
| 330 |
absolute_max=100, |
| 331 |
) |
| 332 |
data = { |
| 333 |
"form-TOTAL_FORMS": "101", |
| 334 |
"form-INITIAL_FORMS": "0", |
| 335 |
"form-MAX_NUM_FORMS": "0", |
| 336 |
} |
| 337 |
formset = GenericFormSet(data=data, prefix="form") |
| 338 |
self.assertIs(formset.is_valid(), False) |
| 339 |
self.assertEqual(len(formset.forms), 100) |
| 340 |
self.assertEqual( |
| 341 |
formset.non_form_errors(), |
| 342 |
["Please submit at most 20 forms."], |
| 343 |
) |
| 344 |
|
| 345 |
def test_can_delete_extra(self): |
| 346 |
GenericFormSet = generic_inlineformset_factory( |
| 347 |
TaggedItem, |
| 348 |
can_delete=True, |
| 349 |
can_delete_extra=True, |
| 350 |
extra=2, |
| 351 |
) |
| 352 |
formset = GenericFormSet() |
| 353 |
self.assertEqual(len(formset), 2) |
| 354 |
self.assertIn("DELETE", formset.forms[0].fields) |
| 355 |
self.assertIn("DELETE", formset.forms[1].fields) |
| 356 |
|
| 357 |
def test_disable_delete_extra(self): |
| 358 |
GenericFormSet = generic_inlineformset_factory( |
| 359 |
TaggedItem, |
| 360 |
can_delete=True, |
| 361 |
can_delete_extra=False, |
| 362 |
extra=2, |
| 363 |
) |
| 364 |
formset = GenericFormSet() |
| 365 |
self.assertEqual(len(formset), 2) |
| 366 |
self.assertNotIn("DELETE", formset.forms[0].fields) |
| 367 |
self.assertNotIn("DELETE", formset.forms[1].fields) |