| 1 |
import datetime |
| 2 |
import re |
| 3 |
from datetime import date |
| 4 |
from decimal import Decimal |
| 5 |
|
| 6 |
from django import forms |
| 7 |
from django.core.exceptions import ImproperlyConfigured |
| 8 |
from django.db import models |
| 9 |
from django.forms.formsets import formset_factory |
| 10 |
from django.forms.models import ( |
| 11 |
BaseModelFormSet, |
| 12 |
ModelForm, |
| 13 |
_get_foreign_key, |
| 14 |
inlineformset_factory, |
| 15 |
modelformset_factory, |
| 16 |
) |
| 17 |
from django.forms.renderers import DjangoTemplates |
| 18 |
from django.http import QueryDict |
| 19 |
from django.test import TestCase, skipUnlessDBFeature |
| 20 |
|
| 21 |
from .models import ( |
| 22 |
AlternateBook, |
| 23 |
Author, |
| 24 |
AuthorMeeting, |
| 25 |
BetterAuthor, |
| 26 |
Book, |
| 27 |
BookWithCustomPK, |
| 28 |
BookWithOptionalAltEditor, |
| 29 |
ClassyMexicanRestaurant, |
| 30 |
CustomPrimaryKey, |
| 31 |
Location, |
| 32 |
Membership, |
| 33 |
MexicanRestaurant, |
| 34 |
Owner, |
| 35 |
OwnerProfile, |
| 36 |
Person, |
| 37 |
Place, |
| 38 |
Player, |
| 39 |
Poem, |
| 40 |
Poet, |
| 41 |
Post, |
| 42 |
Price, |
| 43 |
Product, |
| 44 |
Repository, |
| 45 |
Restaurant, |
| 46 |
Revision, |
| 47 |
Team, |
| 48 |
) |
| 49 |
|
| 50 |
|
| 51 |
class DeletionTests(TestCase): |
| 52 |
def test_deletion(self): |
| 53 |
PoetFormSet = modelformset_factory(Poet, fields="__all__", can_delete=True) |
| 54 |
poet = Poet.objects.create(name="test") |
| 55 |
data = { |
| 56 |
"form-TOTAL_FORMS": "1", |
| 57 |
"form-INITIAL_FORMS": "1", |
| 58 |
"form-MAX_NUM_FORMS": "0", |
| 59 |
"form-0-id": str(poet.pk), |
| 60 |
"form-0-name": "test", |
| 61 |
"form-0-DELETE": "on", |
| 62 |
} |
| 63 |
formset = PoetFormSet(data, queryset=Poet.objects.all()) |
| 64 |
formset.save(commit=False) |
| 65 |
self.assertEqual(Poet.objects.count(), 1) |
| 66 |
|
| 67 |
formset.save() |
| 68 |
self.assertTrue(formset.is_valid()) |
| 69 |
self.assertEqual(Poet.objects.count(), 0) |
| 70 |
|
| 71 |
def test_add_form_deletion_when_invalid(self): |
| 72 |
""" |
| 73 |
Make sure that an add form that is filled out, but marked for deletion |
| 74 |
doesn't cause validation errors. |
| 75 |
""" |
| 76 |
PoetFormSet = modelformset_factory(Poet, fields="__all__", can_delete=True) |
| 77 |
poet = Poet.objects.create(name="test") |
| 78 |
# One existing untouched and two new unvalid forms |
| 79 |
data = { |
| 80 |
"form-TOTAL_FORMS": "3", |
| 81 |
"form-INITIAL_FORMS": "1", |
| 82 |
"form-MAX_NUM_FORMS": "0", |
| 83 |
"form-0-id": str(poet.id), |
| 84 |
"form-0-name": "test", |
| 85 |
"form-1-id": "", |
| 86 |
"form-1-name": "x" * 1000, # Too long |
| 87 |
"form-2-id": str(poet.id), # Violate unique constraint |
| 88 |
"form-2-name": "test2", |
| 89 |
} |
| 90 |
formset = PoetFormSet(data, queryset=Poet.objects.all()) |
| 91 |
# Make sure this form doesn't pass validation. |
| 92 |
self.assertIs(formset.is_valid(), False) |
| 93 |
self.assertEqual(Poet.objects.count(), 1) |
| 94 |
|
| 95 |
# Then make sure that it *does* pass validation and delete the object, |
| 96 |
# even though the data in new forms aren't actually valid. |
| 97 |
data["form-0-DELETE"] = "on" |
| 98 |
data["form-1-DELETE"] = "on" |
| 99 |
data["form-2-DELETE"] = "on" |
| 100 |
formset = PoetFormSet(data, queryset=Poet.objects.all()) |
| 101 |
self.assertIs(formset.is_valid(), True) |
| 102 |
formset.save() |
| 103 |
self.assertEqual(Poet.objects.count(), 0) |
| 104 |
|
| 105 |
def test_change_form_deletion_when_invalid(self): |
| 106 |
""" |
| 107 |
Make sure that a change form that is filled out, but marked for deletion |
| 108 |
doesn't cause validation errors. |
| 109 |
""" |
| 110 |
PoetFormSet = modelformset_factory(Poet, fields="__all__", can_delete=True) |
| 111 |
poet = Poet.objects.create(name="test") |
| 112 |
data = { |
| 113 |
"form-TOTAL_FORMS": "1", |
| 114 |
"form-INITIAL_FORMS": "1", |
| 115 |
"form-MAX_NUM_FORMS": "0", |
| 116 |
"form-0-id": str(poet.id), |
| 117 |
"form-0-name": "x" * 1000, |
| 118 |
} |
| 119 |
formset = PoetFormSet(data, queryset=Poet.objects.all()) |
| 120 |
# Make sure this form doesn't pass validation. |
| 121 |
self.assertIs(formset.is_valid(), False) |
| 122 |
self.assertEqual(Poet.objects.count(), 1) |
| 123 |
|
| 124 |
# Then make sure that it *does* pass validation and delete the object, |
| 125 |
# even though the data isn't actually valid. |
| 126 |
data["form-0-DELETE"] = "on" |
| 127 |
formset = PoetFormSet(data, queryset=Poet.objects.all()) |
| 128 |
self.assertIs(formset.is_valid(), True) |
| 129 |
formset.save() |
| 130 |
self.assertEqual(Poet.objects.count(), 0) |
| 131 |
|
| 132 |
def test_outdated_deletion(self): |
| 133 |
poet = Poet.objects.create(name="test") |
| 134 |
poem = Poem.objects.create(name="Brevity is the soul of wit", poet=poet) |
| 135 |
|
| 136 |
PoemFormSet = inlineformset_factory( |
| 137 |
Poet, Poem, fields="__all__", can_delete=True |
| 138 |
) |
| 139 |
|
| 140 |
# Simulate deletion of an object that doesn't exist in the database |
| 141 |
data = { |
| 142 |
"form-TOTAL_FORMS": "2", |
| 143 |
"form-INITIAL_FORMS": "2", |
| 144 |
"form-0-id": str(poem.pk), |
| 145 |
"form-0-name": "foo", |
| 146 |
"form-1-id": str(poem.pk + 1), # doesn't exist |
| 147 |
"form-1-name": "bar", |
| 148 |
"form-1-DELETE": "on", |
| 149 |
} |
| 150 |
formset = PoemFormSet(data, instance=poet, prefix="form") |
| 151 |
|
| 152 |
# The formset is valid even though poem.pk + 1 doesn't exist, |
| 153 |
# because it's marked for deletion anyway |
| 154 |
self.assertTrue(formset.is_valid()) |
| 155 |
|
| 156 |
formset.save() |
| 157 |
|
| 158 |
# Make sure the save went through correctly |
| 159 |
self.assertEqual(Poem.objects.get(pk=poem.pk).name, "foo") |
| 160 |
self.assertEqual(poet.poem_set.count(), 1) |
| 161 |
self.assertFalse(Poem.objects.filter(pk=poem.pk + 1).exists()) |
| 162 |
|
| 163 |
|
| 164 |
class ModelFormsetTest(TestCase): |
| 165 |
def test_modelformset_factory_without_fields(self): |
| 166 |
"""Regression for #19733""" |
| 167 |
message = ( |
| 168 |
"Calling modelformset_factory without defining 'fields' or 'exclude' " |
| 169 |
"explicitly is prohibited." |
| 170 |
) |
| 171 |
with self.assertRaisesMessage(ImproperlyConfigured, message): |
| 172 |
modelformset_factory(Author) |
| 173 |
|
| 174 |
def test_simple_save(self): |
| 175 |
qs = Author.objects.all() |
| 176 |
AuthorFormSet = modelformset_factory(Author, fields="__all__", extra=3) |
| 177 |
|
| 178 |
formset = AuthorFormSet(queryset=qs) |
| 179 |
self.assertEqual(len(formset.forms), 3) |
| 180 |
self.assertHTMLEqual( |
| 181 |
formset.forms[0].as_p(), |
| 182 |
'<p><label for="id_form-0-name">Name:</label>' |
| 183 |
'<input id="id_form-0-name" type="text" name="form-0-name" maxlength="100">' |
| 184 |
'<input type="hidden" name="form-0-id" id="id_form-0-id"></p>', |
| 185 |
) |
| 186 |
self.assertHTMLEqual( |
| 187 |
formset.forms[1].as_p(), |
| 188 |
'<p><label for="id_form-1-name">Name:</label>' |
| 189 |
'<input id="id_form-1-name" type="text" name="form-1-name" maxlength="100">' |
| 190 |
'<input type="hidden" name="form-1-id" id="id_form-1-id"></p>', |
| 191 |
) |
| 192 |
self.assertHTMLEqual( |
| 193 |
formset.forms[2].as_p(), |
| 194 |
'<p><label for="id_form-2-name">Name:</label>' |
| 195 |
'<input id="id_form-2-name" type="text" name="form-2-name" maxlength="100">' |
| 196 |
'<input type="hidden" name="form-2-id" id="id_form-2-id"></p>', |
| 197 |
) |
| 198 |
|
| 199 |
data = { |
| 200 |
"form-TOTAL_FORMS": "3", # the number of forms rendered |
| 201 |
"form-INITIAL_FORMS": "0", # the number of forms with initial data |
| 202 |
"form-MAX_NUM_FORMS": "", # the max number of forms |
| 203 |
"form-0-name": "Charles Baudelaire", |
| 204 |
"form-1-name": "Arthur Rimbaud", |
| 205 |
"form-2-name": "", |
| 206 |
} |
| 207 |
|
| 208 |
formset = AuthorFormSet(data=data, queryset=qs) |
| 209 |
self.assertTrue(formset.is_valid()) |
| 210 |
|
| 211 |
saved = formset.save() |
| 212 |
self.assertEqual(len(saved), 2) |
| 213 |
author1, author2 = saved |
| 214 |
self.assertEqual(author1, Author.objects.get(name="Charles Baudelaire")) |
| 215 |
self.assertEqual(author2, Author.objects.get(name="Arthur Rimbaud")) |
| 216 |
|
| 217 |
authors = list(Author.objects.order_by("name")) |
| 218 |
self.assertEqual(authors, [author2, author1]) |
| 219 |
|
| 220 |
# Gah! We forgot Paul Verlaine. Let's create a formset to edit the |
| 221 |
# existing authors with an extra form to add him. We *could* pass in a |
| 222 |
# queryset to restrict the Author objects we edit, but in this case |
| 223 |
# we'll use it to display them in alphabetical order by name. |
| 224 |
|
| 225 |
qs = Author.objects.order_by("name") |
| 226 |
AuthorFormSet = modelformset_factory( |
| 227 |
Author, fields="__all__", extra=1, can_delete=False |
| 228 |
) |
| 229 |
|
| 230 |
formset = AuthorFormSet(queryset=qs) |
| 231 |
self.assertEqual(len(formset.forms), 3) |
| 232 |
self.assertHTMLEqual( |
| 233 |
formset.forms[0].as_p(), |
| 234 |
'<p><label for="id_form-0-name">Name:</label>' |
| 235 |
'<input id="id_form-0-name" type="text" name="form-0-name" ' |
| 236 |
'value="Arthur Rimbaud" maxlength="100">' |
| 237 |
'<input type="hidden" name="form-0-id" value="%d" id="id_form-0-id"></p>' |
| 238 |
% author2.id, |
| 239 |
) |
| 240 |
self.assertHTMLEqual( |
| 241 |
formset.forms[1].as_p(), |
| 242 |
'<p><label for="id_form-1-name">Name:</label>' |
| 243 |
'<input id="id_form-1-name" type="text" name="form-1-name" ' |
| 244 |
'value="Charles Baudelaire" maxlength="100">' |
| 245 |
'<input type="hidden" name="form-1-id" value="%d" id="id_form-1-id"></p>' |
| 246 |
% author1.id, |
| 247 |
) |
| 248 |
self.assertHTMLEqual( |
| 249 |
formset.forms[2].as_p(), |
| 250 |
'<p><label for="id_form-2-name">Name:</label>' |
| 251 |
'<input id="id_form-2-name" type="text" name="form-2-name" maxlength="100">' |
| 252 |
'<input type="hidden" name="form-2-id" id="id_form-2-id"></p>', |
| 253 |
) |
| 254 |
|
| 255 |
data = { |
| 256 |
"form-TOTAL_FORMS": "3", # the number of forms rendered |
| 257 |
"form-INITIAL_FORMS": "2", # the number of forms with initial data |
| 258 |
"form-MAX_NUM_FORMS": "", # the max number of forms |
| 259 |
"form-0-id": str(author2.id), |
| 260 |
"form-0-name": "Arthur Rimbaud", |
| 261 |
"form-1-id": str(author1.id), |
| 262 |
"form-1-name": "Charles Baudelaire", |
| 263 |
"form-2-name": "Paul Verlaine", |
| 264 |
} |
| 265 |
|
| 266 |
formset = AuthorFormSet(data=data, queryset=qs) |
| 267 |
self.assertTrue(formset.is_valid()) |
| 268 |
|
| 269 |
# Only changed or new objects are returned from formset.save() |
| 270 |
saved = formset.save() |
| 271 |
self.assertEqual(len(saved), 1) |
| 272 |
author3 = saved[0] |
| 273 |
self.assertEqual(author3, Author.objects.get(name="Paul Verlaine")) |
| 274 |
|
| 275 |
authors = list(Author.objects.order_by("name")) |
| 276 |
self.assertEqual(authors, [author2, author1, author3]) |
| 277 |
|
| 278 |
# This probably shouldn't happen, but it will. If an add form was |
| 279 |
# marked for deletion, make sure we don't save that form. |
| 280 |
|
| 281 |
qs = Author.objects.order_by("name") |
| 282 |
AuthorFormSet = modelformset_factory( |
| 283 |
Author, fields="__all__", extra=1, can_delete=True |
| 284 |
) |
| 285 |
|
| 286 |
formset = AuthorFormSet(queryset=qs) |
| 287 |
self.assertEqual(len(formset.forms), 4) |
| 288 |
self.assertHTMLEqual( |
| 289 |
formset.forms[0].as_p(), |
| 290 |
'<p><label for="id_form-0-name">Name:</label>' |
| 291 |
'<input id="id_form-0-name" type="text" name="form-0-name" ' |
| 292 |
'value="Arthur Rimbaud" maxlength="100"></p>' |
| 293 |
'<p><label for="id_form-0-DELETE">Delete:</label>' |
| 294 |
'<input type="checkbox" name="form-0-DELETE" id="id_form-0-DELETE">' |
| 295 |
'<input type="hidden" name="form-0-id" value="%d" id="id_form-0-id"></p>' |
| 296 |
% author2.id, |
| 297 |
) |
| 298 |
self.assertHTMLEqual( |
| 299 |
formset.forms[1].as_p(), |
| 300 |
'<p><label for="id_form-1-name">Name:</label>' |
| 301 |
'<input id="id_form-1-name" type="text" name="form-1-name" ' |
| 302 |
'value="Charles Baudelaire" maxlength="100"></p>' |
| 303 |
'<p><label for="id_form-1-DELETE">Delete:</label>' |
| 304 |
'<input type="checkbox" name="form-1-DELETE" id="id_form-1-DELETE">' |
| 305 |
'<input type="hidden" name="form-1-id" value="%d" id="id_form-1-id"></p>' |
| 306 |
% author1.id, |
| 307 |
) |
| 308 |
self.assertHTMLEqual( |
| 309 |
formset.forms[2].as_p(), |
| 310 |
'<p><label for="id_form-2-name">Name:</label>' |
| 311 |
'<input id="id_form-2-name" type="text" name="form-2-name" ' |
| 312 |
'value="Paul Verlaine" maxlength="100"></p>' |
| 313 |
'<p><label for="id_form-2-DELETE">Delete:</label>' |
| 314 |
'<input type="checkbox" name="form-2-DELETE" id="id_form-2-DELETE">' |
| 315 |
'<input type="hidden" name="form-2-id" value="%d" id="id_form-2-id"></p>' |
| 316 |
% author3.id, |
| 317 |
) |
| 318 |
self.assertHTMLEqual( |
| 319 |
formset.forms[3].as_p(), |
| 320 |
'<p><label for="id_form-3-name">Name:</label>' |
| 321 |
'<input id="id_form-3-name" type="text" name="form-3-name" maxlength="100">' |
| 322 |
'</p><p><label for="id_form-3-DELETE">Delete:</label>' |
| 323 |
'<input type="checkbox" name="form-3-DELETE" id="id_form-3-DELETE">' |
| 324 |
'<input type="hidden" name="form-3-id" id="id_form-3-id"></p>', |
| 325 |
) |
| 326 |
|
| 327 |
data = { |
| 328 |
"form-TOTAL_FORMS": "4", # the number of forms rendered |
| 329 |
"form-INITIAL_FORMS": "3", # the number of forms with initial data |
| 330 |
"form-MAX_NUM_FORMS": "", # the max number of forms |
| 331 |
"form-0-id": str(author2.id), |
| 332 |
"form-0-name": "Arthur Rimbaud", |
| 333 |
"form-1-id": str(author1.id), |
| 334 |
"form-1-name": "Charles Baudelaire", |
| 335 |
"form-2-id": str(author3.id), |
| 336 |
"form-2-name": "Paul Verlaine", |
| 337 |
"form-3-name": "Walt Whitman", |
| 338 |
"form-3-DELETE": "on", |
| 339 |
} |
| 340 |
|
| 341 |
formset = AuthorFormSet(data=data, queryset=qs) |
| 342 |
self.assertTrue(formset.is_valid()) |
| 343 |
|
| 344 |
# No objects were changed or saved so nothing will come back. |
| 345 |
|
| 346 |
self.assertEqual(formset.save(), []) |
| 347 |
|
| 348 |
authors = list(Author.objects.order_by("name")) |
| 349 |
self.assertEqual(authors, [author2, author1, author3]) |
| 350 |
|
| 351 |
# Let's edit a record to ensure save only returns that one record. |
| 352 |
|
| 353 |
data = { |
| 354 |
"form-TOTAL_FORMS": "4", # the number of forms rendered |
| 355 |
"form-INITIAL_FORMS": "3", # the number of forms with initial data |
| 356 |
"form-MAX_NUM_FORMS": "", # the max number of forms |
| 357 |
"form-0-id": str(author2.id), |
| 358 |
"form-0-name": "Walt Whitman", |
| 359 |
"form-1-id": str(author1.id), |
| 360 |
"form-1-name": "Charles Baudelaire", |
| 361 |
"form-2-id": str(author3.id), |
| 362 |
"form-2-name": "Paul Verlaine", |
| 363 |
"form-3-name": "", |
| 364 |
"form-3-DELETE": "", |
| 365 |
} |
| 366 |
|
| 367 |
formset = AuthorFormSet(data=data, queryset=qs) |
| 368 |
self.assertTrue(formset.is_valid()) |
| 369 |
|
| 370 |
# One record has changed. |
| 371 |
|
| 372 |
saved = formset.save() |
| 373 |
self.assertEqual(len(saved), 1) |
| 374 |
self.assertEqual(saved[0], Author.objects.get(name="Walt Whitman")) |
| 375 |
|
| 376 |
def test_commit_false(self): |
| 377 |
# Test the behavior of commit=False and save_m2m |
| 378 |
|
| 379 |
author1 = Author.objects.create(name="Charles Baudelaire") |
| 380 |
author2 = Author.objects.create(name="Paul Verlaine") |
| 381 |
author3 = Author.objects.create(name="Walt Whitman") |
| 382 |
|
| 383 |
meeting = AuthorMeeting.objects.create(created=date.today()) |
| 384 |
meeting.authors.set(Author.objects.all()) |
| 385 |
|
| 386 |
# create an Author instance to add to the meeting. |
| 387 |
|
| 388 |
author4 = Author.objects.create(name="John Steinbeck") |
| 389 |
|
| 390 |
AuthorMeetingFormSet = modelformset_factory( |
| 391 |
AuthorMeeting, fields="__all__", extra=1, can_delete=True |
| 392 |
) |
| 393 |
data = { |
| 394 |
"form-TOTAL_FORMS": "2", # the number of forms rendered |
| 395 |
"form-INITIAL_FORMS": "1", # the number of forms with initial data |
| 396 |
"form-MAX_NUM_FORMS": "", # the max number of forms |
| 397 |
"form-0-id": str(meeting.id), |
| 398 |
"form-0-name": "2nd Tuesday of the Week Meeting", |
| 399 |
"form-0-authors": [author2.id, author1.id, author3.id, author4.id], |
| 400 |
"form-1-name": "", |
| 401 |
"form-1-authors": "", |
| 402 |
"form-1-DELETE": "", |
| 403 |
} |
| 404 |
formset = AuthorMeetingFormSet(data=data, queryset=AuthorMeeting.objects.all()) |
| 405 |
self.assertTrue(formset.is_valid()) |
| 406 |
|
| 407 |
instances = formset.save(commit=False) |
| 408 |
for instance in instances: |
| 409 |
instance.created = date.today() |
| 410 |
instance.save() |
| 411 |
formset.save_m2m() |
| 412 |
self.assertSequenceEqual( |
| 413 |
instances[0].authors.all(), |
| 414 |
[author1, author4, author2, author3], |
| 415 |
) |
| 416 |
|
| 417 |
def test_max_num(self): |
| 418 |
# Test the behavior of max_num with model formsets. It should allow |
| 419 |
# all existing related objects/inlines for a given object to be |
| 420 |
# displayed, but not allow the creation of new inlines beyond max_num. |
| 421 |
|
| 422 |
a1 = Author.objects.create(name="Charles Baudelaire") |
| 423 |
a2 = Author.objects.create(name="Paul Verlaine") |
| 424 |
a3 = Author.objects.create(name="Walt Whitman") |
| 425 |
|
| 426 |
qs = Author.objects.order_by("name") |
| 427 |
|
| 428 |
AuthorFormSet = modelformset_factory( |
| 429 |
Author, fields="__all__", max_num=None, extra=3 |
| 430 |
) |
| 431 |
formset = AuthorFormSet(queryset=qs) |
| 432 |
self.assertEqual(len(formset.forms), 6) |
| 433 |
self.assertEqual(len(formset.extra_forms), 3) |
| 434 |
|
| 435 |
AuthorFormSet = modelformset_factory( |
| 436 |
Author, fields="__all__", max_num=4, extra=3 |
| 437 |
) |
| 438 |
formset = AuthorFormSet(queryset=qs) |
| 439 |
self.assertEqual(len(formset.forms), 4) |
| 440 |
self.assertEqual(len(formset.extra_forms), 1) |
| 441 |
|
| 442 |
AuthorFormSet = modelformset_factory( |
| 443 |
Author, fields="__all__", max_num=0, extra=3 |
| 444 |
) |
| 445 |
formset = AuthorFormSet(queryset=qs) |
| 446 |
self.assertEqual(len(formset.forms), 3) |
| 447 |
self.assertEqual(len(formset.extra_forms), 0) |
| 448 |
|
| 449 |
AuthorFormSet = modelformset_factory(Author, fields="__all__", max_num=None) |
| 450 |
formset = AuthorFormSet(queryset=qs) |
| 451 |
self.assertSequenceEqual(formset.get_queryset(), [a1, a2, a3]) |
| 452 |
|
| 453 |
AuthorFormSet = modelformset_factory(Author, fields="__all__", max_num=0) |
| 454 |
formset = AuthorFormSet(queryset=qs) |
| 455 |
self.assertSequenceEqual(formset.get_queryset(), [a1, a2, a3]) |
| 456 |
|
| 457 |
AuthorFormSet = modelformset_factory(Author, fields="__all__", max_num=4) |
| 458 |
formset = AuthorFormSet(queryset=qs) |
| 459 |
self.assertSequenceEqual(formset.get_queryset(), [a1, a2, a3]) |
| 460 |
|
| 461 |
def test_min_num(self): |
| 462 |
# Test the behavior of min_num with model formsets. It should be |
| 463 |
# added to extra. |
| 464 |
qs = Author.objects.none() |
| 465 |
|
| 466 |
AuthorFormSet = modelformset_factory(Author, fields="__all__", extra=0) |
| 467 |
formset = AuthorFormSet(queryset=qs) |
| 468 |
self.assertEqual(len(formset.forms), 0) |
| 469 |
|
| 470 |
AuthorFormSet = modelformset_factory( |
| 471 |
Author, fields="__all__", min_num=1, extra=0 |
| 472 |
) |
| 473 |
formset = AuthorFormSet(queryset=qs) |
| 474 |
self.assertEqual(len(formset.forms), 1) |
| 475 |
|
| 476 |
AuthorFormSet = modelformset_factory( |
| 477 |
Author, fields="__all__", min_num=1, extra=1 |
| 478 |
) |
| 479 |
formset = AuthorFormSet(queryset=qs) |
| 480 |
self.assertEqual(len(formset.forms), 2) |
| 481 |
|
| 482 |
def test_min_num_with_existing(self): |
| 483 |
# Test the behavior of min_num with existing objects. |
| 484 |
Author.objects.create(name="Charles Baudelaire") |
| 485 |
qs = Author.objects.all() |
| 486 |
|
| 487 |
AuthorFormSet = modelformset_factory( |
| 488 |
Author, fields="__all__", extra=0, min_num=1 |
| 489 |
) |
| 490 |
formset = AuthorFormSet(queryset=qs) |
| 491 |
self.assertEqual(len(formset.forms), 1) |
| 492 |
|
| 493 |
def test_custom_save_method(self): |
| 494 |
class PoetForm(forms.ModelForm): |
| 495 |
def save(self, commit=True): |
| 496 |
# change the name to "Vladimir Mayakovsky" just to be a jerk. |
| 497 |
author = super().save(commit=False) |
| 498 |
author.name = "Vladimir Mayakovsky" |
| 499 |
if commit: |
| 500 |
author.save() |
| 501 |
return author |
| 502 |
|
| 503 |
PoetFormSet = modelformset_factory(Poet, fields="__all__", form=PoetForm) |
| 504 |
|
| 505 |
data = { |
| 506 |
"form-TOTAL_FORMS": "3", # the number of forms rendered |
| 507 |
"form-INITIAL_FORMS": "0", # the number of forms with initial data |
| 508 |
"form-MAX_NUM_FORMS": "", # the max number of forms |
| 509 |
"form-0-name": "Walt Whitman", |
| 510 |
"form-1-name": "Charles Baudelaire", |
| 511 |
"form-2-name": "", |
| 512 |
} |
| 513 |
|
| 514 |
qs = Poet.objects.all() |
| 515 |
formset = PoetFormSet(data=data, queryset=qs) |
| 516 |
self.assertTrue(formset.is_valid()) |
| 517 |
|
| 518 |
poets = formset.save() |
| 519 |
self.assertEqual(len(poets), 2) |
| 520 |
poet1, poet2 = poets |
| 521 |
self.assertEqual(poet1.name, "Vladimir Mayakovsky") |
| 522 |
self.assertEqual(poet2.name, "Vladimir Mayakovsky") |
| 523 |
|
| 524 |
def test_custom_form(self): |
| 525 |
""" |
| 526 |
model_formset_factory() respects fields and exclude parameters of a |
| 527 |
custom form. |
| 528 |
""" |
| 529 |
|
| 530 |
class PostForm1(forms.ModelForm): |
| 531 |
class Meta: |
| 532 |
model = Post |
| 533 |
fields = ("title", "posted") |
| 534 |
|
| 535 |
class PostForm2(forms.ModelForm): |
| 536 |
class Meta: |
| 537 |
model = Post |
| 538 |
exclude = ("subtitle",) |
| 539 |
|
| 540 |
PostFormSet = modelformset_factory(Post, form=PostForm1) |
| 541 |
formset = PostFormSet() |
| 542 |
self.assertNotIn("subtitle", formset.forms[0].fields) |
| 543 |
|
| 544 |
PostFormSet = modelformset_factory(Post, form=PostForm2) |
| 545 |
formset = PostFormSet() |
| 546 |
self.assertNotIn("subtitle", formset.forms[0].fields) |
| 547 |
|
| 548 |
def test_custom_queryset_init(self): |
| 549 |
""" |
| 550 |
A queryset can be overridden in the formset's __init__() method. |
| 551 |
""" |
| 552 |
Author.objects.create(name="Charles Baudelaire") |
| 553 |
Author.objects.create(name="Paul Verlaine") |
| 554 |
|
| 555 |
class BaseAuthorFormSet(BaseModelFormSet): |
| 556 |
def __init__(self, *args, **kwargs): |
| 557 |
super().__init__(*args, **kwargs) |
| 558 |
self.queryset = Author.objects.filter(name__startswith="Charles") |
| 559 |
|
| 560 |
AuthorFormSet = modelformset_factory( |
| 561 |
Author, fields="__all__", formset=BaseAuthorFormSet |
| 562 |
) |
| 563 |
formset = AuthorFormSet() |
| 564 |
self.assertEqual(len(formset.get_queryset()), 1) |
| 565 |
|
| 566 |
def test_model_inheritance(self): |
| 567 |
BetterAuthorFormSet = modelformset_factory(BetterAuthor, fields="__all__") |
| 568 |
formset = BetterAuthorFormSet() |
| 569 |
self.assertEqual(len(formset.forms), 1) |
| 570 |
self.assertHTMLEqual( |
| 571 |
formset.forms[0].as_p(), |
| 572 |
'<p><label for="id_form-0-name">Name:</label>' |
| 573 |
'<input id="id_form-0-name" type="text" name="form-0-name" maxlength="100">' |
| 574 |
'</p><p><label for="id_form-0-write_speed">Write speed:</label>' |
| 575 |
'<input type="number" name="form-0-write_speed" id="id_form-0-write_speed">' |
| 576 |
'<input type="hidden" name="form-0-author_ptr" id="id_form-0-author_ptr">' |
| 577 |
"</p>", |
| 578 |
) |
| 579 |
|
| 580 |
data = { |
| 581 |
"form-TOTAL_FORMS": "1", # the number of forms rendered |
| 582 |
"form-INITIAL_FORMS": "0", # the number of forms with initial data |
| 583 |
"form-MAX_NUM_FORMS": "", # the max number of forms |
| 584 |
"form-0-author_ptr": "", |
| 585 |
"form-0-name": "Ernest Hemingway", |
| 586 |
"form-0-write_speed": "10", |
| 587 |
} |
| 588 |
|
| 589 |
formset = BetterAuthorFormSet(data) |
| 590 |
self.assertTrue(formset.is_valid()) |
| 591 |
saved = formset.save() |
| 592 |
self.assertEqual(len(saved), 1) |
| 593 |
(author1,) = saved |
| 594 |
self.assertEqual(author1, BetterAuthor.objects.get(name="Ernest Hemingway")) |
| 595 |
hemingway_id = BetterAuthor.objects.get(name="Ernest Hemingway").pk |
| 596 |
|
| 597 |
formset = BetterAuthorFormSet() |
| 598 |
self.assertEqual(len(formset.forms), 2) |
| 599 |
self.assertHTMLEqual( |
| 600 |
formset.forms[0].as_p(), |
| 601 |
'<p><label for="id_form-0-name">Name:</label>' |
| 602 |
'<input id="id_form-0-name" type="text" name="form-0-name" ' |
| 603 |
'value="Ernest Hemingway" maxlength="100"></p>' |
| 604 |
'<p><label for="id_form-0-write_speed">Write speed:</label>' |
| 605 |
'<input type="number" name="form-0-write_speed" value="10" ' |
| 606 |
'id="id_form-0-write_speed">' |
| 607 |
'<input type="hidden" name="form-0-author_ptr" value="%d" ' |
| 608 |
'id="id_form-0-author_ptr"></p>' % hemingway_id, |
| 609 |
) |
| 610 |
self.assertHTMLEqual( |
| 611 |
formset.forms[1].as_p(), |
| 612 |
'<p><label for="id_form-1-name">Name:</label>' |
| 613 |
'<input id="id_form-1-name" type="text" name="form-1-name" maxlength="100">' |
| 614 |
'</p><p><label for="id_form-1-write_speed">Write speed:</label>' |
| 615 |
'<input type="number" name="form-1-write_speed" id="id_form-1-write_speed">' |
| 616 |
'<input type="hidden" name="form-1-author_ptr" id="id_form-1-author_ptr">' |
| 617 |
"</p>", |
| 618 |
) |
| 619 |
|
| 620 |
data = { |
| 621 |
"form-TOTAL_FORMS": "2", # the number of forms rendered |
| 622 |
"form-INITIAL_FORMS": "1", # the number of forms with initial data |
| 623 |
"form-MAX_NUM_FORMS": "", # the max number of forms |
| 624 |
"form-0-author_ptr": hemingway_id, |
| 625 |
"form-0-name": "Ernest Hemingway", |
| 626 |
"form-0-write_speed": "10", |
| 627 |
"form-1-author_ptr": "", |
| 628 |
"form-1-name": "", |
| 629 |
"form-1-write_speed": "", |
| 630 |
} |
| 631 |
|
| 632 |
formset = BetterAuthorFormSet(data) |
| 633 |
self.assertTrue(formset.is_valid()) |
| 634 |
self.assertEqual(formset.save(), []) |
| 635 |
|
| 636 |
def test_inline_formsets(self): |
| 637 |
# We can also create a formset that is tied to a parent model. This is |
| 638 |
# how the admin system's edit inline functionality works. |
| 639 |
|
| 640 |
AuthorBooksFormSet = inlineformset_factory( |
| 641 |
Author, Book, can_delete=False, extra=3, fields="__all__" |
| 642 |
) |
| 643 |
author = Author.objects.create(name="Charles Baudelaire") |
| 644 |
|
| 645 |
formset = AuthorBooksFormSet(instance=author) |
| 646 |
self.assertEqual(len(formset.forms), 3) |
| 647 |
self.assertHTMLEqual( |
| 648 |
formset.forms[0].as_p(), |
| 649 |
'<p><label for="id_book_set-0-title">Title:</label>' |
| 650 |
'<input id="id_book_set-0-title" type="text" name="book_set-0-title" ' |
| 651 |
'maxlength="100">' |
| 652 |
'<input type="hidden" name="book_set-0-author" value="%d" ' |
| 653 |
'id="id_book_set-0-author">' |
| 654 |
'<input type="hidden" name="book_set-0-id" id="id_book_set-0-id">' |
| 655 |
"</p>" % author.id, |
| 656 |
) |
| 657 |
self.assertHTMLEqual( |
| 658 |
formset.forms[1].as_p(), |
| 659 |
'<p><label for="id_book_set-1-title">Title:</label>' |
| 660 |
'<input id="id_book_set-1-title" type="text" name="book_set-1-title" ' |
| 661 |
'maxlength="100">' |
| 662 |
'<input type="hidden" name="book_set-1-author" value="%d" ' |
| 663 |
'id="id_book_set-1-author">' |
| 664 |
'<input type="hidden" name="book_set-1-id" id="id_book_set-1-id"></p>' |
| 665 |
% author.id, |
| 666 |
) |
| 667 |
self.assertHTMLEqual( |
| 668 |
formset.forms[2].as_p(), |
| 669 |
'<p><label for="id_book_set-2-title">Title:</label>' |
| 670 |
'<input id="id_book_set-2-title" type="text" name="book_set-2-title" ' |
| 671 |
'maxlength="100">' |
| 672 |
'<input type="hidden" name="book_set-2-author" value="%d" ' |
| 673 |
'id="id_book_set-2-author">' |
| 674 |
'<input type="hidden" name="book_set-2-id" id="id_book_set-2-id"></p>' |
| 675 |
% author.id, |
| 676 |
) |
| 677 |
|
| 678 |
data = { |
| 679 |
"book_set-TOTAL_FORMS": "3", # the number of forms rendered |
| 680 |
"book_set-INITIAL_FORMS": "0", # the number of forms with initial data |
| 681 |
"book_set-MAX_NUM_FORMS": "", # the max number of forms |
| 682 |
"book_set-0-title": "Les Fleurs du Mal", |
| 683 |
"book_set-1-title": "", |
| 684 |
"book_set-2-title": "", |
| 685 |
} |
| 686 |
|
| 687 |
formset = AuthorBooksFormSet(data, instance=author) |
| 688 |
self.assertTrue(formset.is_valid()) |
| 689 |
|
| 690 |
saved = formset.save() |
| 691 |
self.assertEqual(len(saved), 1) |
| 692 |
(book1,) = saved |
| 693 |
self.assertEqual(book1, Book.objects.get(title="Les Fleurs du Mal")) |
| 694 |
self.assertSequenceEqual(author.book_set.all(), [book1]) |
| 695 |
|
| 696 |
# Now that we've added a book to Charles Baudelaire, let's try adding |
| 697 |
# another one. This time though, an edit form will be available for |
| 698 |
# every existing book. |
| 699 |
|
| 700 |
AuthorBooksFormSet = inlineformset_factory( |
| 701 |
Author, Book, can_delete=False, extra=2, fields="__all__" |
| 702 |
) |
| 703 |
author = Author.objects.get(name="Charles Baudelaire") |
| 704 |
|
| 705 |
formset = AuthorBooksFormSet(instance=author) |
| 706 |
self.assertEqual(len(formset.forms), 3) |
| 707 |
self.assertHTMLEqual( |
| 708 |
formset.forms[0].as_p(), |
| 709 |
'<p><label for="id_book_set-0-title">Title:</label>' |
| 710 |
'<input id="id_book_set-0-title" type="text" name="book_set-0-title" ' |
| 711 |
'value="Les Fleurs du Mal" maxlength="100">' |
| 712 |
'<input type="hidden" name="book_set-0-author" value="%d" ' |
| 713 |
'id="id_book_set-0-author">' |
| 714 |
'<input type="hidden" name="book_set-0-id" value="%d" ' |
| 715 |
'id="id_book_set-0-id"></p>' |
| 716 |
% ( |
| 717 |
author.id, |
| 718 |
book1.id, |
| 719 |
), |
| 720 |
) |
| 721 |
self.assertHTMLEqual( |
| 722 |
formset.forms[1].as_p(), |
| 723 |
'<p><label for="id_book_set-1-title">Title:</label>' |
| 724 |
'<input id="id_book_set-1-title" type="text" name="book_set-1-title" ' |
| 725 |
'maxlength="100">' |
| 726 |
'<input type="hidden" name="book_set-1-author" value="%d" ' |
| 727 |
'id="id_book_set-1-author">' |
| 728 |
'<input type="hidden" name="book_set-1-id" id="id_book_set-1-id"></p>' |
| 729 |
% author.id, |
| 730 |
) |
| 731 |
self.assertHTMLEqual( |
| 732 |
formset.forms[2].as_p(), |
| 733 |
'<p><label for="id_book_set-2-title">Title:</label>' |
| 734 |
'<input id="id_book_set-2-title" type="text" name="book_set-2-title" ' |
| 735 |
'maxlength="100">' |
| 736 |
'<input type="hidden" name="book_set-2-author" value="%d" ' |
| 737 |
'id="id_book_set-2-author">' |
| 738 |
'<input type="hidden" name="book_set-2-id" id="id_book_set-2-id"></p>' |
| 739 |
% author.id, |
| 740 |
) |
| 741 |
|
| 742 |
data = { |
| 743 |
"book_set-TOTAL_FORMS": "3", # the number of forms rendered |
| 744 |
"book_set-INITIAL_FORMS": "1", # the number of forms with initial data |
| 745 |
"book_set-MAX_NUM_FORMS": "", # the max number of forms |
| 746 |
"book_set-0-id": str(book1.id), |
| 747 |
"book_set-0-title": "Les Fleurs du Mal", |
| 748 |
"book_set-1-title": "Les Paradis Artificiels", |
| 749 |
"book_set-2-title": "", |
| 750 |
} |
| 751 |
|
| 752 |
formset = AuthorBooksFormSet(data, instance=author) |
| 753 |
self.assertTrue(formset.is_valid()) |
| 754 |
|
| 755 |
saved = formset.save() |
| 756 |
self.assertEqual(len(saved), 1) |
| 757 |
(book2,) = saved |
| 758 |
self.assertEqual(book2, Book.objects.get(title="Les Paradis Artificiels")) |
| 759 |
|
| 760 |
# As you can see, 'Les Paradis Artificiels' is now a book belonging to |
| 761 |
# Charles Baudelaire. |
| 762 |
self.assertSequenceEqual(author.book_set.order_by("title"), [book1, book2]) |
| 763 |
|
| 764 |
def test_inline_formsets_save_as_new(self): |
| 765 |
# The save_as_new parameter lets you re-associate the data to a new |
| 766 |
# instance. This is used in the admin for save_as functionality. |
| 767 |
AuthorBooksFormSet = inlineformset_factory( |
| 768 |
Author, Book, can_delete=False, extra=2, fields="__all__" |
| 769 |
) |
| 770 |
Author.objects.create(name="Charles Baudelaire") |
| 771 |
|
| 772 |
# An immutable QueryDict simulates request.POST. |
| 773 |
data = QueryDict(mutable=True) |
| 774 |
data.update( |
| 775 |
{ |
| 776 |
"book_set-TOTAL_FORMS": "3", # the number of forms rendered |
| 777 |
"book_set-INITIAL_FORMS": "2", # the number of forms with initial data |
| 778 |
"book_set-MAX_NUM_FORMS": "", # the max number of forms |
| 779 |
"book_set-0-id": "1", |
| 780 |
"book_set-0-title": "Les Fleurs du Mal", |
| 781 |
"book_set-1-id": "2", |
| 782 |
"book_set-1-title": "Les Paradis Artificiels", |
| 783 |
"book_set-2-title": "", |
| 784 |
} |
| 785 |
) |
| 786 |
data._mutable = False |
| 787 |
|
| 788 |
formset = AuthorBooksFormSet(data, instance=Author(), save_as_new=True) |
| 789 |
self.assertTrue(formset.is_valid()) |
| 790 |
self.assertIs(data._mutable, False) |
| 791 |
|
| 792 |
new_author = Author.objects.create(name="Charles Baudelaire") |
| 793 |
formset = AuthorBooksFormSet(data, instance=new_author, save_as_new=True) |
| 794 |
saved = formset.save() |
| 795 |
self.assertEqual(len(saved), 2) |
| 796 |
book1, book2 = saved |
| 797 |
self.assertEqual(book1.title, "Les Fleurs du Mal") |
| 798 |
self.assertEqual(book2.title, "Les Paradis Artificiels") |
| 799 |
|
| 800 |
# Test using a custom prefix on an inline formset. |
| 801 |
|
| 802 |
formset = AuthorBooksFormSet(prefix="test") |
| 803 |
self.assertEqual(len(formset.forms), 2) |
| 804 |
self.assertHTMLEqual( |
| 805 |
formset.forms[0].as_p(), |
| 806 |
'<p><label for="id_test-0-title">Title:</label>' |
| 807 |
'<input id="id_test-0-title" type="text" name="test-0-title" ' |
| 808 |
'maxlength="100">' |
| 809 |
'<input type="hidden" name="test-0-author" id="id_test-0-author">' |
| 810 |
'<input type="hidden" name="test-0-id" id="id_test-0-id"></p>', |
| 811 |
) |
| 812 |
|
| 813 |
self.assertHTMLEqual( |
| 814 |
formset.forms[1].as_p(), |
| 815 |
'<p><label for="id_test-1-title">Title:</label>' |
| 816 |
'<input id="id_test-1-title" type="text" name="test-1-title" ' |
| 817 |
'maxlength="100">' |
| 818 |
'<input type="hidden" name="test-1-author" id="id_test-1-author">' |
| 819 |
'<input type="hidden" name="test-1-id" id="id_test-1-id"></p>', |
| 820 |
) |
| 821 |
|
| 822 |
def test_inline_formsets_with_custom_pk(self): |
| 823 |
# Test inline formsets where the inline-edited object has a custom |
| 824 |
# primary key that is not the fk to the parent object. |
| 825 |
self.maxDiff = 1024 |
| 826 |
|
| 827 |
AuthorBooksFormSet2 = inlineformset_factory( |
| 828 |
Author, BookWithCustomPK, can_delete=False, extra=1, fields="__all__" |
| 829 |
) |
| 830 |
author = Author.objects.create(pk=1, name="Charles Baudelaire") |
| 831 |
|
| 832 |
formset = AuthorBooksFormSet2(instance=author) |
| 833 |
self.assertEqual(len(formset.forms), 1) |
| 834 |
self.assertHTMLEqual( |
| 835 |
formset.forms[0].as_p(), |
| 836 |
'<p><label for="id_bookwithcustompk_set-0-my_pk">My pk:</label>' |
| 837 |
'<input id="id_bookwithcustompk_set-0-my_pk" type="number" ' |
| 838 |
'name="bookwithcustompk_set-0-my_pk" step="1"></p>' |
| 839 |
'<p><label for="id_bookwithcustompk_set-0-title">Title:</label>' |
| 840 |
'<input id="id_bookwithcustompk_set-0-title" type="text" ' |
| 841 |
'name="bookwithcustompk_set-0-title" maxlength="100">' |
| 842 |
'<input type="hidden" name="bookwithcustompk_set-0-author" ' |
| 843 |
'value="1" id="id_bookwithcustompk_set-0-author"></p>', |
| 844 |
) |
| 845 |
|
| 846 |
data = { |
| 847 |
# The number of forms rendered. |
| 848 |
"bookwithcustompk_set-TOTAL_FORMS": "1", |
| 849 |
# The number of forms with initial data. |
| 850 |
"bookwithcustompk_set-INITIAL_FORMS": "0", |
| 851 |
# The max number of forms. |
| 852 |
"bookwithcustompk_set-MAX_NUM_FORMS": "", |
| 853 |
"bookwithcustompk_set-0-my_pk": "77777", |
| 854 |
"bookwithcustompk_set-0-title": "Les Fleurs du Mal", |
| 855 |
} |
| 856 |
|
| 857 |
formset = AuthorBooksFormSet2(data, instance=author) |
| 858 |
self.assertTrue(formset.is_valid()) |
| 859 |
|
| 860 |
saved = formset.save() |
| 861 |
self.assertEqual(len(saved), 1) |
| 862 |
(book1,) = saved |
| 863 |
self.assertEqual(book1.pk, 77777) |
| 864 |
|
| 865 |
book1 = author.bookwithcustompk_set.get() |
| 866 |
self.assertEqual(book1.title, "Les Fleurs du Mal") |
| 867 |
|
| 868 |
def test_inline_formsets_with_multi_table_inheritance(self): |
| 869 |
# Test inline formsets where the inline-edited object uses multi-table |
| 870 |
# inheritance, thus has a non AutoField yet auto-created primary key. |
| 871 |
|
| 872 |
AuthorBooksFormSet3 = inlineformset_factory( |
| 873 |
Author, AlternateBook, can_delete=False, extra=1, fields="__all__" |
| 874 |
) |
| 875 |
author = Author.objects.create(pk=1, name="Charles Baudelaire") |
| 876 |
|
| 877 |
formset = AuthorBooksFormSet3(instance=author) |
| 878 |
self.assertEqual(len(formset.forms), 1) |
| 879 |
self.assertHTMLEqual( |
| 880 |
formset.forms[0].as_p(), |
| 881 |
'<p><label for="id_alternatebook_set-0-title">Title:</label>' |
| 882 |
'<input id="id_alternatebook_set-0-title" type="text" ' |
| 883 |
'name="alternatebook_set-0-title" maxlength="100"></p>' |
| 884 |
'<p><label for="id_alternatebook_set-0-notes">Notes:</label>' |
| 885 |
'<input id="id_alternatebook_set-0-notes" type="text" ' |
| 886 |
'name="alternatebook_set-0-notes" maxlength="100">' |
| 887 |
'<input type="hidden" name="alternatebook_set-0-author" value="1" ' |
| 888 |
'id="id_alternatebook_set-0-author">' |
| 889 |
'<input type="hidden" name="alternatebook_set-0-book_ptr" ' |
| 890 |
'id="id_alternatebook_set-0-book_ptr"></p>', |
| 891 |
) |
| 892 |
|
| 893 |
data = { |
| 894 |
# The number of forms rendered. |
| 895 |
"alternatebook_set-TOTAL_FORMS": "1", |
| 896 |
# The number of forms with initial data. |
| 897 |
"alternatebook_set-INITIAL_FORMS": "0", |
| 898 |
# The max number of forms. |
| 899 |
"alternatebook_set-MAX_NUM_FORMS": "", |
| 900 |
"alternatebook_set-0-title": "Flowers of Evil", |
| 901 |
"alternatebook_set-0-notes": "English translation of Les Fleurs du Mal", |
| 902 |
} |
| 903 |
|
| 904 |
formset = AuthorBooksFormSet3(data, instance=author) |
| 905 |
self.assertTrue(formset.is_valid()) |
| 906 |
|
| 907 |
saved = formset.save() |
| 908 |
self.assertEqual(len(saved), 1) |
| 909 |
(book1,) = saved |
| 910 |
self.assertEqual(book1.title, "Flowers of Evil") |
| 911 |
self.assertEqual(book1.notes, "English translation of Les Fleurs du Mal") |
| 912 |
|
| 913 |
@skipUnlessDBFeature("supports_partially_nullable_unique_constraints") |
| 914 |
def test_inline_formsets_with_nullable_unique_together(self): |
| 915 |
# Test inline formsets where the inline-edited object has a |
| 916 |
# unique_together constraint with a nullable member |
| 917 |
|
| 918 |
AuthorBooksFormSet4 = inlineformset_factory( |
| 919 |
Author, |
| 920 |
BookWithOptionalAltEditor, |
| 921 |
can_delete=False, |
| 922 |
extra=2, |
| 923 |
fields="__all__", |
| 924 |
) |
| 925 |
author = Author.objects.create(pk=1, name="Charles Baudelaire") |
| 926 |
|
| 927 |
data = { |
| 928 |
# The number of forms rendered. |
| 929 |
"bookwithoptionalalteditor_set-TOTAL_FORMS": "2", |
| 930 |
# The number of forms with initial data. |
| 931 |
"bookwithoptionalalteditor_set-INITIAL_FORMS": "0", |
| 932 |
# The max number of forms. |
| 933 |
"bookwithoptionalalteditor_set-MAX_NUM_FORMS": "", |
| 934 |
"bookwithoptionalalteditor_set-0-author": "1", |
| 935 |
"bookwithoptionalalteditor_set-0-title": "Les Fleurs du Mal", |
| 936 |
"bookwithoptionalalteditor_set-1-author": "1", |
| 937 |
"bookwithoptionalalteditor_set-1-title": "Les Fleurs du Mal", |
| 938 |
} |
| 939 |
formset = AuthorBooksFormSet4(data, instance=author) |
| 940 |
self.assertTrue(formset.is_valid()) |
| 941 |
|
| 942 |
saved = formset.save() |
| 943 |
self.assertEqual(len(saved), 2) |
| 944 |
book1, book2 = saved |
| 945 |
self.assertEqual(book1.author_id, 1) |
| 946 |
self.assertEqual(book1.title, "Les Fleurs du Mal") |
| 947 |
self.assertEqual(book2.author_id, 1) |
| 948 |
self.assertEqual(book2.title, "Les Fleurs du Mal") |
| 949 |
|
| 950 |
def test_inline_formsets_with_custom_save_method(self): |
| 951 |
AuthorBooksFormSet = inlineformset_factory( |
| 952 |
Author, Book, can_delete=False, extra=2, fields="__all__" |
| 953 |
) |
| 954 |
author = Author.objects.create(pk=1, name="Charles Baudelaire") |
| 955 |
book1 = Book.objects.create( |
| 956 |
pk=1, author=author, title="Les Paradis Artificiels" |
| 957 |
) |
| 958 |
book2 = Book.objects.create(pk=2, author=author, title="Les Fleurs du Mal") |
| 959 |
book3 = Book.objects.create(pk=3, author=author, title="Flowers of Evil") |
| 960 |
|
| 961 |
class PoemForm(forms.ModelForm): |
| 962 |
def save(self, commit=True): |
| 963 |
# change the name to "Brooklyn Bridge" just to be a jerk. |
| 964 |
poem = super().save(commit=False) |
| 965 |
poem.name = "Brooklyn Bridge" |
| 966 |
if commit: |
| 967 |
poem.save() |
| 968 |
return poem |
| 969 |
|
| 970 |
PoemFormSet = inlineformset_factory(Poet, Poem, form=PoemForm, fields="__all__") |
| 971 |
|
| 972 |
data = { |
| 973 |
"poem_set-TOTAL_FORMS": "3", # the number of forms rendered |
| 974 |
"poem_set-INITIAL_FORMS": "0", # the number of forms with initial data |
| 975 |
"poem_set-MAX_NUM_FORMS": "", # the max number of forms |
| 976 |
"poem_set-0-name": "The Cloud in Trousers", |
| 977 |
"poem_set-1-name": "I", |
| 978 |
"poem_set-2-name": "", |
| 979 |
} |
| 980 |
|
| 981 |
poet = Poet.objects.create(name="Vladimir Mayakovsky") |
| 982 |
formset = PoemFormSet(data=data, instance=poet) |
| 983 |
self.assertTrue(formset.is_valid()) |
| 984 |
|
| 985 |
saved = formset.save() |
| 986 |
self.assertEqual(len(saved), 2) |
| 987 |
poem1, poem2 = saved |
| 988 |
self.assertEqual(poem1.name, "Brooklyn Bridge") |
| 989 |
self.assertEqual(poem2.name, "Brooklyn Bridge") |
| 990 |
|
| 991 |
# We can provide a custom queryset to our InlineFormSet: |
| 992 |
|
| 993 |
custom_qs = Book.objects.order_by("-title") |
| 994 |
formset = AuthorBooksFormSet(instance=author, queryset=custom_qs) |
| 995 |
self.assertEqual(len(formset.forms), 5) |
| 996 |
self.assertHTMLEqual( |
| 997 |
formset.forms[0].as_p(), |
| 998 |
'<p><label for="id_book_set-0-title">Title:</label>' |
| 999 |
'<input id="id_book_set-0-title" type="text" name="book_set-0-title" ' |
| 1000 |
'value="Les Paradis Artificiels" maxlength="100">' |
| 1001 |
'<input type="hidden" name="book_set-0-author" value="1" ' |
| 1002 |
'id="id_book_set-0-author">' |
| 1003 |
'<input type="hidden" name="book_set-0-id" value="1" id="id_book_set-0-id">' |
| 1004 |
"</p>", |
| 1005 |
) |
| 1006 |
self.assertHTMLEqual( |
| 1007 |
formset.forms[1].as_p(), |
| 1008 |
'<p><label for="id_book_set-1-title">Title:</label>' |
| 1009 |
'<input id="id_book_set-1-title" type="text" name="book_set-1-title" ' |
| 1010 |
'value="Les Fleurs du Mal" maxlength="100">' |
| 1011 |
'<input type="hidden" name="book_set-1-author" value="1" ' |
| 1012 |
'id="id_book_set-1-author">' |
| 1013 |
'<input type="hidden" name="book_set-1-id" value="2" id="id_book_set-1-id">' |
| 1014 |
"</p>", |
| 1015 |
) |
| 1016 |
self.assertHTMLEqual( |
| 1017 |
formset.forms[2].as_p(), |
| 1018 |
'<p><label for="id_book_set-2-title">Title:</label>' |
| 1019 |
'<input id="id_book_set-2-title" type="text" name="book_set-2-title" ' |
| 1020 |
'value="Flowers of Evil" maxlength="100">' |
| 1021 |
'<input type="hidden" name="book_set-2-author" value="1" ' |
| 1022 |
'id="id_book_set-2-author">' |
| 1023 |
'<input type="hidden" name="book_set-2-id" value="3" ' |
| 1024 |
'id="id_book_set-2-id"></p>', |
| 1025 |
) |
| 1026 |
self.assertHTMLEqual( |
| 1027 |
formset.forms[3].as_p(), |
| 1028 |
'<p><label for="id_book_set-3-title">Title:</label>' |
| 1029 |
'<input id="id_book_set-3-title" type="text" name="book_set-3-title" ' |
| 1030 |
'maxlength="100">' |
| 1031 |
'<input type="hidden" name="book_set-3-author" value="1" ' |
| 1032 |
'id="id_book_set-3-author">' |
| 1033 |
'<input type="hidden" name="book_set-3-id" id="id_book_set-3-id"></p>', |
| 1034 |
) |
| 1035 |
self.assertHTMLEqual( |
| 1036 |
formset.forms[4].as_p(), |
| 1037 |
'<p><label for="id_book_set-4-title">Title:</label>' |
| 1038 |
'<input id="id_book_set-4-title" type="text" name="book_set-4-title" ' |
| 1039 |
'maxlength="100">' |
| 1040 |
'<input type="hidden" name="book_set-4-author" value="1" ' |
| 1041 |
'id="id_book_set-4-author">' |
| 1042 |
'<input type="hidden" name="book_set-4-id" id="id_book_set-4-id"></p>', |
| 1043 |
) |
| 1044 |
|
| 1045 |
data = { |
| 1046 |
"book_set-TOTAL_FORMS": "5", # the number of forms rendered |
| 1047 |
"book_set-INITIAL_FORMS": "3", # the number of forms with initial data |
| 1048 |
"book_set-MAX_NUM_FORMS": "", # the max number of forms |
| 1049 |
"book_set-0-id": str(book1.id), |
| 1050 |
"book_set-0-title": "Les Paradis Artificiels", |
| 1051 |
"book_set-1-id": str(book2.id), |
| 1052 |
"book_set-1-title": "Les Fleurs du Mal", |
| 1053 |
"book_set-2-id": str(book3.id), |
| 1054 |
"book_set-2-title": "Flowers of Evil", |
| 1055 |
"book_set-3-title": "Revue des deux mondes", |
| 1056 |
"book_set-4-title": "", |
| 1057 |
} |
| 1058 |
formset = AuthorBooksFormSet(data, instance=author, queryset=custom_qs) |
| 1059 |
self.assertTrue(formset.is_valid()) |
| 1060 |
|
| 1061 |
custom_qs = Book.objects.filter(title__startswith="F") |
| 1062 |
formset = AuthorBooksFormSet(instance=author, queryset=custom_qs) |
| 1063 |
self.assertHTMLEqual( |
| 1064 |
formset.forms[0].as_p(), |
| 1065 |
'<p><label for="id_book_set-0-title">Title:</label>' |
| 1066 |
'<input id="id_book_set-0-title" type="text" name="book_set-0-title" ' |
| 1067 |
'value="Flowers of Evil" maxlength="100">' |
| 1068 |
'<input type="hidden" name="book_set-0-author" value="1" ' |
| 1069 |
'id="id_book_set-0-author">' |
| 1070 |
'<input type="hidden" name="book_set-0-id" value="3" ' |
| 1071 |
'id="id_book_set-0-id"></p>', |
| 1072 |
) |
| 1073 |
self.assertHTMLEqual( |
| 1074 |
formset.forms[1].as_p(), |
| 1075 |
'<p><label for="id_book_set-1-title">Title:</label>' |
| 1076 |
'<input id="id_book_set-1-title" type="text" name="book_set-1-title" ' |
| 1077 |
'maxlength="100">' |
| 1078 |
'<input type="hidden" name="book_set-1-author" value="1" ' |
| 1079 |
'id="id_book_set-1-author">' |
| 1080 |
'<input type="hidden" name="book_set-1-id" id="id_book_set-1-id"></p>', |
| 1081 |
) |
| 1082 |
self.assertHTMLEqual( |
| 1083 |
formset.forms[2].as_p(), |
| 1084 |
'<p><label for="id_book_set-2-title">Title:</label>' |
| 1085 |
'<input id="id_book_set-2-title" type="text" name="book_set-2-title" ' |
| 1086 |
'maxlength="100">' |
| 1087 |
'<input type="hidden" name="book_set-2-author" value="1" ' |
| 1088 |
'id="id_book_set-2-author">' |
| 1089 |
'<input type="hidden" name="book_set-2-id" id="id_book_set-2-id"></p>', |
| 1090 |
) |
| 1091 |
|
| 1092 |
data = { |
| 1093 |
"book_set-TOTAL_FORMS": "3", # the number of forms rendered |
| 1094 |
"book_set-INITIAL_FORMS": "1", # the number of forms with initial data |
| 1095 |
"book_set-MAX_NUM_FORMS": "", # the max number of forms |
| 1096 |
"book_set-0-id": str(book3.id), |
| 1097 |
"book_set-0-title": "Flowers of Evil", |
| 1098 |
"book_set-1-title": "Revue des deux mondes", |
| 1099 |
"book_set-2-title": "", |
| 1100 |
} |
| 1101 |
formset = AuthorBooksFormSet(data, instance=author, queryset=custom_qs) |
| 1102 |
self.assertTrue(formset.is_valid()) |
| 1103 |
|
| 1104 |
def test_inline_formsets_with_custom_save_method_related_instance(self): |
| 1105 |
""" |
| 1106 |
The ModelForm.save() method should be able to access the related object |
| 1107 |
if it exists in the database (#24395). |
| 1108 |
""" |
| 1109 |
|
| 1110 |
class PoemForm2(forms.ModelForm): |
| 1111 |
def save(self, commit=True): |
| 1112 |
poem = super().save(commit=False) |
| 1113 |
poem.name = "%s by %s" % (poem.name, poem.poet.name) |
| 1114 |
if commit: |
| 1115 |
poem.save() |
| 1116 |
return poem |
| 1117 |
|
| 1118 |
PoemFormSet = inlineformset_factory( |
| 1119 |
Poet, Poem, form=PoemForm2, fields="__all__" |
| 1120 |
) |
| 1121 |
data = { |
| 1122 |
"poem_set-TOTAL_FORMS": "1", |
| 1123 |
"poem_set-INITIAL_FORMS": "0", |
| 1124 |
"poem_set-MAX_NUM_FORMS": "", |
| 1125 |
"poem_set-0-name": "Le Lac", |
| 1126 |
} |
| 1127 |
poet = Poet() |
| 1128 |
formset = PoemFormSet(data=data, instance=poet) |
| 1129 |
self.assertTrue(formset.is_valid()) |
| 1130 |
|
| 1131 |
# The Poet instance is saved after the formset instantiation. This |
| 1132 |
# happens in admin's changeform_view() when adding a new object and |
| 1133 |
# some inlines in the same request. |
| 1134 |
poet.name = "Lamartine" |
| 1135 |
poet.save() |
| 1136 |
poem = formset.save()[0] |
| 1137 |
self.assertEqual(poem.name, "Le Lac by Lamartine") |
| 1138 |
|
| 1139 |
def test_inline_formsets_with_wrong_fk_name(self): |
| 1140 |
"""Regression for #23451""" |
| 1141 |
message = "fk_name 'title' is not a ForeignKey to 'model_formsets.Author'." |
| 1142 |
with self.assertRaisesMessage(ValueError, message): |
| 1143 |
inlineformset_factory(Author, Book, fields="__all__", fk_name="title") |
| 1144 |
|
| 1145 |
def test_custom_pk(self): |
| 1146 |
# We need to ensure that it is displayed |
| 1147 |
|
| 1148 |
CustomPrimaryKeyFormSet = modelformset_factory( |
| 1149 |
CustomPrimaryKey, fields="__all__" |
| 1150 |
) |
| 1151 |
formset = CustomPrimaryKeyFormSet() |
| 1152 |
self.assertEqual(len(formset.forms), 1) |
| 1153 |
self.assertHTMLEqual( |
| 1154 |
formset.forms[0].as_p(), |
| 1155 |
'<p><label for="id_form-0-my_pk">My pk:</label>' |
| 1156 |
'<input id="id_form-0-my_pk" type="text" name="form-0-my_pk" ' |
| 1157 |
'maxlength="10"></p>' |
| 1158 |
'<p><label for="id_form-0-some_field">Some field:</label>' |
| 1159 |
'<input id="id_form-0-some_field" type="text" name="form-0-some_field" ' |
| 1160 |
'maxlength="100"></p>', |
| 1161 |
) |
| 1162 |
|
| 1163 |
# Custom primary keys with ForeignKey, OneToOneField and AutoField ############ |
| 1164 |
|
| 1165 |
place = Place.objects.create(pk=1, name="Giordanos", city="Chicago") |
| 1166 |
|
| 1167 |
FormSet = inlineformset_factory( |
| 1168 |
Place, Owner, extra=2, can_delete=False, fields="__all__" |
| 1169 |
) |
| 1170 |
formset = FormSet(instance=place) |
| 1171 |
self.assertEqual(len(formset.forms), 2) |
| 1172 |
self.assertHTMLEqual( |
| 1173 |
formset.forms[0].as_p(), |
| 1174 |
'<p><label for="id_owner_set-0-name">Name:</label>' |
| 1175 |
'<input id="id_owner_set-0-name" type="text" name="owner_set-0-name" ' |
| 1176 |
'maxlength="100">' |
| 1177 |
'<input type="hidden" name="owner_set-0-place" value="1" ' |
| 1178 |
'id="id_owner_set-0-place">' |
| 1179 |
'<input type="hidden" name="owner_set-0-auto_id" ' |
| 1180 |
'id="id_owner_set-0-auto_id"></p>', |
| 1181 |
) |
| 1182 |
self.assertHTMLEqual( |
| 1183 |
formset.forms[1].as_p(), |
| 1184 |
'<p><label for="id_owner_set-1-name">Name:</label>' |
| 1185 |
'<input id="id_owner_set-1-name" type="text" name="owner_set-1-name" ' |
| 1186 |
'maxlength="100">' |
| 1187 |
'<input type="hidden" name="owner_set-1-place" value="1" ' |
| 1188 |
'id="id_owner_set-1-place">' |
| 1189 |
'<input type="hidden" name="owner_set-1-auto_id" ' |
| 1190 |
'id="id_owner_set-1-auto_id"></p>', |
| 1191 |
) |
| 1192 |
|
| 1193 |
data = { |
| 1194 |
"owner_set-TOTAL_FORMS": "2", |
| 1195 |
"owner_set-INITIAL_FORMS": "0", |
| 1196 |
"owner_set-MAX_NUM_FORMS": "", |
| 1197 |
"owner_set-0-auto_id": "", |
| 1198 |
"owner_set-0-name": "Joe Perry", |
| 1199 |
"owner_set-1-auto_id": "", |
| 1200 |
"owner_set-1-name": "", |
| 1201 |
} |
| 1202 |
formset = FormSet(data, instance=place) |
| 1203 |
self.assertTrue(formset.is_valid()) |
| 1204 |
saved = formset.save() |
| 1205 |
self.assertEqual(len(saved), 1) |
| 1206 |
(owner1,) = saved |
| 1207 |
self.assertEqual(owner1.name, "Joe Perry") |
| 1208 |
self.assertEqual(owner1.place.name, "Giordanos") |
| 1209 |
|
| 1210 |
formset = FormSet(instance=place) |
| 1211 |
self.assertEqual(len(formset.forms), 3) |
| 1212 |
self.assertHTMLEqual( |
| 1213 |
formset.forms[0].as_p(), |
| 1214 |
'<p><label for="id_owner_set-0-name">Name:</label>' |
| 1215 |
'<input id="id_owner_set-0-name" type="text" name="owner_set-0-name" ' |
| 1216 |
'value="Joe Perry" maxlength="100">' |
| 1217 |
'<input type="hidden" name="owner_set-0-place" value="1" ' |
| 1218 |
'id="id_owner_set-0-place">' |
| 1219 |
'<input type="hidden" name="owner_set-0-auto_id" value="%d" ' |
| 1220 |
'id="id_owner_set-0-auto_id"></p>' % owner1.auto_id, |
| 1221 |
) |
| 1222 |
self.assertHTMLEqual( |
| 1223 |
formset.forms[1].as_p(), |
| 1224 |
'<p><label for="id_owner_set-1-name">Name:</label>' |
| 1225 |
'<input id="id_owner_set-1-name" type="text" name="owner_set-1-name" ' |
| 1226 |
'maxlength="100">' |
| 1227 |
'<input type="hidden" name="owner_set-1-place" value="1" ' |
| 1228 |
'id="id_owner_set-1-place">' |
| 1229 |
'<input type="hidden" name="owner_set-1-auto_id" ' |
| 1230 |
'id="id_owner_set-1-auto_id"></p>', |
| 1231 |
) |
| 1232 |
self.assertHTMLEqual( |
| 1233 |
formset.forms[2].as_p(), |
| 1234 |
'<p><label for="id_owner_set-2-name">Name:</label>' |
| 1235 |
'<input id="id_owner_set-2-name" type="text" name="owner_set-2-name" ' |
| 1236 |
'maxlength="100">' |
| 1237 |
'<input type="hidden" name="owner_set-2-place" value="1" ' |
| 1238 |
'id="id_owner_set-2-place">' |
| 1239 |
'<input type="hidden" name="owner_set-2-auto_id" ' |
| 1240 |
'id="id_owner_set-2-auto_id"></p>', |
| 1241 |
) |
| 1242 |
|
| 1243 |
data = { |
| 1244 |
"owner_set-TOTAL_FORMS": "3", |
| 1245 |
"owner_set-INITIAL_FORMS": "1", |
| 1246 |
"owner_set-MAX_NUM_FORMS": "", |
| 1247 |
"owner_set-0-auto_id": str(owner1.auto_id), |
| 1248 |
"owner_set-0-name": "Joe Perry", |
| 1249 |
"owner_set-1-auto_id": "", |
| 1250 |
"owner_set-1-name": "Jack Berry", |
| 1251 |
"owner_set-2-auto_id": "", |
| 1252 |
"owner_set-2-name": "", |
| 1253 |
} |
| 1254 |
formset = FormSet(data, instance=place) |
| 1255 |
self.assertTrue(formset.is_valid()) |
| 1256 |
saved = formset.save() |
| 1257 |
self.assertEqual(len(saved), 1) |
| 1258 |
(owner2,) = saved |
| 1259 |
self.assertEqual(owner2.name, "Jack Berry") |
| 1260 |
self.assertEqual(owner2.place.name, "Giordanos") |
| 1261 |
|
| 1262 |
# A custom primary key that is a ForeignKey or OneToOneField get |
| 1263 |
# rendered for the user to choose. |
| 1264 |
FormSet = modelformset_factory(OwnerProfile, fields="__all__") |
| 1265 |
formset = FormSet() |
| 1266 |
self.assertHTMLEqual( |
| 1267 |
formset.forms[0].as_p(), |
| 1268 |
'<p><label for="id_form-0-owner">Owner:</label>' |
| 1269 |
'<select name="form-0-owner" id="id_form-0-owner">' |
| 1270 |
'<option value="" selected>---------</option>' |
| 1271 |
'<option value="%d">Joe Perry at Giordanos</option>' |
| 1272 |
'<option value="%d">Jack Berry at Giordanos</option>' |
| 1273 |
"</select></p>" |
| 1274 |
'<p><label for="id_form-0-age">Age:</label>' |
| 1275 |
'<input type="number" name="form-0-age" id="id_form-0-age" min="0"></p>' |
| 1276 |
% (owner1.auto_id, owner2.auto_id), |
| 1277 |
) |
| 1278 |
|
| 1279 |
owner1 = Owner.objects.get(name="Joe Perry") |
| 1280 |
FormSet = inlineformset_factory( |
| 1281 |
Owner, OwnerProfile, max_num=1, can_delete=False, fields="__all__" |
| 1282 |
) |
| 1283 |
self.assertEqual(FormSet.max_num, 1) |
| 1284 |
|
| 1285 |
formset = FormSet(instance=owner1) |
| 1286 |
self.assertEqual(len(formset.forms), 1) |
| 1287 |
self.assertHTMLEqual( |
| 1288 |
formset.forms[0].as_p(), |
| 1289 |
'<p><label for="id_ownerprofile-0-age">Age:</label>' |
| 1290 |
'<input type="number" name="ownerprofile-0-age" ' |
| 1291 |
'id="id_ownerprofile-0-age" min="0">' |
| 1292 |
'<input type="hidden" name="ownerprofile-0-owner" value="%d" ' |
| 1293 |
'id="id_ownerprofile-0-owner"></p>' % owner1.auto_id, |
| 1294 |
) |
| 1295 |
|
| 1296 |
data = { |
| 1297 |
"ownerprofile-TOTAL_FORMS": "1", |
| 1298 |
"ownerprofile-INITIAL_FORMS": "0", |
| 1299 |
"ownerprofile-MAX_NUM_FORMS": "1", |
| 1300 |
"ownerprofile-0-owner": "", |
| 1301 |
"ownerprofile-0-age": "54", |
| 1302 |
} |
| 1303 |
formset = FormSet(data, instance=owner1) |
| 1304 |
self.assertTrue(formset.is_valid()) |
| 1305 |
saved = formset.save() |
| 1306 |
self.assertEqual(len(saved), 1) |
| 1307 |
(profile1,) = saved |
| 1308 |
self.assertEqual(profile1.owner, owner1) |
| 1309 |
self.assertEqual(profile1.age, 54) |
| 1310 |
|
| 1311 |
formset = FormSet(instance=owner1) |
| 1312 |
self.assertEqual(len(formset.forms), 1) |
| 1313 |
self.assertHTMLEqual( |
| 1314 |
formset.forms[0].as_p(), |
| 1315 |
'<p><label for="id_ownerprofile-0-age">Age:</label>' |
| 1316 |
'<input type="number" name="ownerprofile-0-age" value="54" ' |
| 1317 |
'id="id_ownerprofile-0-age" min="0">' |
| 1318 |
'<input type="hidden" name="ownerprofile-0-owner" value="%d" ' |
| 1319 |
'id="id_ownerprofile-0-owner"></p>' % owner1.auto_id, |
| 1320 |
) |
| 1321 |
|
| 1322 |
data = { |
| 1323 |
"ownerprofile-TOTAL_FORMS": "1", |
| 1324 |
"ownerprofile-INITIAL_FORMS": "1", |
| 1325 |
"ownerprofile-MAX_NUM_FORMS": "1", |
| 1326 |
"ownerprofile-0-owner": str(owner1.auto_id), |
| 1327 |
"ownerprofile-0-age": "55", |
| 1328 |
} |
| 1329 |
formset = FormSet(data, instance=owner1) |
| 1330 |
self.assertTrue(formset.is_valid()) |
| 1331 |
saved = formset.save() |
| 1332 |
self.assertEqual(len(saved), 1) |
| 1333 |
(profile1,) = saved |
| 1334 |
self.assertEqual(profile1.owner, owner1) |
| 1335 |
self.assertEqual(profile1.age, 55) |
| 1336 |
|
| 1337 |
def test_unique_true_enforces_max_num_one(self): |
| 1338 |
# ForeignKey with unique=True should enforce max_num=1 |
| 1339 |
|
| 1340 |
place = Place.objects.create(pk=1, name="Giordanos", city="Chicago") |
| 1341 |
|
| 1342 |
FormSet = inlineformset_factory( |
| 1343 |
Place, Location, can_delete=False, fields="__all__" |
| 1344 |
) |
| 1345 |
self.assertEqual(FormSet.max_num, 1) |
| 1346 |
|
| 1347 |
formset = FormSet(instance=place) |
| 1348 |
self.assertEqual(len(formset.forms), 1) |
| 1349 |
self.assertHTMLEqual( |
| 1350 |
formset.forms[0].as_p(), |
| 1351 |
'<p><label for="id_location_set-0-lat">Lat:</label>' |
| 1352 |
'<input id="id_location_set-0-lat" type="text" name="location_set-0-lat" ' |
| 1353 |
'maxlength="100"></p>' |
| 1354 |
'<p><label for="id_location_set-0-lon">Lon:</label>' |
| 1355 |
'<input id="id_location_set-0-lon" type="text" name="location_set-0-lon" ' |
| 1356 |
'maxlength="100">' |
| 1357 |
'<input type="hidden" name="location_set-0-place" value="1" ' |
| 1358 |
'id="id_location_set-0-place">' |
| 1359 |
'<input type="hidden" name="location_set-0-id" ' |
| 1360 |
'id="id_location_set-0-id"></p>', |
| 1361 |
) |
| 1362 |
|
| 1363 |
def test_foreign_keys_in_parents(self): |
| 1364 |
self.assertEqual(type(_get_foreign_key(Restaurant, Owner)), models.ForeignKey) |
| 1365 |
self.assertEqual( |
| 1366 |
type(_get_foreign_key(MexicanRestaurant, Owner)), models.ForeignKey |
| 1367 |
) |
| 1368 |
|
| 1369 |
def test_unique_validation(self): |
| 1370 |
FormSet = modelformset_factory(Product, fields="__all__", extra=1) |
| 1371 |
data = { |
| 1372 |
"form-TOTAL_FORMS": "1", |
| 1373 |
"form-INITIAL_FORMS": "0", |
| 1374 |
"form-MAX_NUM_FORMS": "", |
| 1375 |
"form-0-slug": "car-red", |
| 1376 |
} |
| 1377 |
formset = FormSet(data) |
| 1378 |
self.assertTrue(formset.is_valid()) |
| 1379 |
saved = formset.save() |
| 1380 |
self.assertEqual(len(saved), 1) |
| 1381 |
(product1,) = saved |
| 1382 |
self.assertEqual(product1.slug, "car-red") |
| 1383 |
|
| 1384 |
data = { |
| 1385 |
"form-TOTAL_FORMS": "1", |
| 1386 |
"form-INITIAL_FORMS": "0", |
| 1387 |
"form-MAX_NUM_FORMS": "", |
| 1388 |
"form-0-slug": "car-red", |
| 1389 |
} |
| 1390 |
formset = FormSet(data) |
| 1391 |
self.assertFalse(formset.is_valid()) |
| 1392 |
self.assertEqual( |
| 1393 |
formset.errors, [{"slug": ["Product with this Slug already exists."]}] |
| 1394 |
) |
| 1395 |
|
| 1396 |
def test_modelformset_validate_max_flag(self): |
| 1397 |
# If validate_max is set and max_num is less than TOTAL_FORMS in the |
| 1398 |
# data, then throw an exception. MAX_NUM_FORMS in the data is |
| 1399 |
# irrelevant here (it's output as a hint for the client but its |
| 1400 |
# value in the returned data is not checked) |
| 1401 |
|
| 1402 |
data = { |
| 1403 |
"form-TOTAL_FORMS": "2", |
| 1404 |
"form-INITIAL_FORMS": "0", |
| 1405 |
"form-MAX_NUM_FORMS": "2", # should be ignored |
| 1406 |
"form-0-price": "12.00", |
| 1407 |
"form-0-quantity": "1", |
| 1408 |
"form-1-price": "24.00", |
| 1409 |
"form-1-quantity": "2", |
| 1410 |
} |
| 1411 |
|
| 1412 |
FormSet = modelformset_factory( |
| 1413 |
Price, fields="__all__", extra=1, max_num=1, validate_max=True |
| 1414 |
) |
| 1415 |
formset = FormSet(data) |
| 1416 |
self.assertFalse(formset.is_valid()) |
| 1417 |
self.assertEqual(formset.non_form_errors(), ["Please submit at most 1 form."]) |
| 1418 |
|
| 1419 |
# Now test the same thing without the validate_max flag to ensure |
| 1420 |
# default behavior is unchanged |
| 1421 |
FormSet = modelformset_factory(Price, fields="__all__", extra=1, max_num=1) |
| 1422 |
formset = FormSet(data) |
| 1423 |
self.assertTrue(formset.is_valid()) |
| 1424 |
|
| 1425 |
def test_modelformset_min_num_equals_max_num_less_than(self): |
| 1426 |
data = { |
| 1427 |
"form-TOTAL_FORMS": "3", |
| 1428 |
"form-INITIAL_FORMS": "0", |
| 1429 |
"form-MAX_NUM_FORMS": "2", |
| 1430 |
"form-0-slug": "car-red", |
| 1431 |
"form-1-slug": "car-blue", |
| 1432 |
"form-2-slug": "car-black", |
| 1433 |
} |
| 1434 |
FormSet = modelformset_factory( |
| 1435 |
Product, |
| 1436 |
fields="__all__", |
| 1437 |
extra=1, |
| 1438 |
max_num=2, |
| 1439 |
validate_max=True, |
| 1440 |
min_num=2, |
| 1441 |
validate_min=True, |
| 1442 |
) |
| 1443 |
formset = FormSet(data) |
| 1444 |
self.assertFalse(formset.is_valid()) |
| 1445 |
self.assertEqual(formset.non_form_errors(), ["Please submit at most 2 forms."]) |
| 1446 |
|
| 1447 |
def test_modelformset_min_num_equals_max_num_more_than(self): |
| 1448 |
data = { |
| 1449 |
"form-TOTAL_FORMS": "1", |
| 1450 |
"form-INITIAL_FORMS": "0", |
| 1451 |
"form-MAX_NUM_FORMS": "2", |
| 1452 |
"form-0-slug": "car-red", |
| 1453 |
} |
| 1454 |
FormSet = modelformset_factory( |
| 1455 |
Product, |
| 1456 |
fields="__all__", |
| 1457 |
extra=1, |
| 1458 |
max_num=2, |
| 1459 |
validate_max=True, |
| 1460 |
min_num=2, |
| 1461 |
validate_min=True, |
| 1462 |
) |
| 1463 |
formset = FormSet(data) |
| 1464 |
self.assertFalse(formset.is_valid()) |
| 1465 |
self.assertEqual(formset.non_form_errors(), ["Please submit at least 2 forms."]) |
| 1466 |
|
| 1467 |
def test_unique_together_validation(self): |
| 1468 |
FormSet = modelformset_factory(Price, fields="__all__", extra=1) |
| 1469 |
data = { |
| 1470 |
"form-TOTAL_FORMS": "1", |
| 1471 |
"form-INITIAL_FORMS": "0", |
| 1472 |
"form-MAX_NUM_FORMS": "", |
| 1473 |
"form-0-price": "12.00", |
| 1474 |
"form-0-quantity": "1", |
| 1475 |
} |
| 1476 |
formset = FormSet(data) |
| 1477 |
self.assertTrue(formset.is_valid()) |
| 1478 |
saved = formset.save() |
| 1479 |
self.assertEqual(len(saved), 1) |
| 1480 |
(price1,) = saved |
| 1481 |
self.assertEqual(price1.price, Decimal("12.00")) |
| 1482 |
self.assertEqual(price1.quantity, 1) |
| 1483 |
|
| 1484 |
data = { |
| 1485 |
"form-TOTAL_FORMS": "1", |
| 1486 |
"form-INITIAL_FORMS": "0", |
| 1487 |
"form-MAX_NUM_FORMS": "", |
| 1488 |
"form-0-price": "12.00", |
| 1489 |
"form-0-quantity": "1", |
| 1490 |
} |
| 1491 |
formset = FormSet(data) |
| 1492 |
self.assertFalse(formset.is_valid()) |
| 1493 |
self.assertEqual( |
| 1494 |
formset.errors, |
| 1495 |
[{"__all__": ["Price with this Price and Quantity already exists."]}], |
| 1496 |
) |
| 1497 |
|
| 1498 |
def test_unique_together_with_inlineformset_factory(self): |
| 1499 |
# Also see bug #8882. |
| 1500 |
|
| 1501 |
repository = Repository.objects.create(name="Test Repo") |
| 1502 |
FormSet = inlineformset_factory(Repository, Revision, extra=1, fields="__all__") |
| 1503 |
data = { |
| 1504 |
"revision_set-TOTAL_FORMS": "1", |
| 1505 |
"revision_set-INITIAL_FORMS": "0", |
| 1506 |
"revision_set-MAX_NUM_FORMS": "", |
| 1507 |
"revision_set-0-repository": repository.pk, |
| 1508 |
"revision_set-0-revision": "146239817507f148d448db38840db7c3cbf47c76", |
| 1509 |
"revision_set-0-DELETE": "", |
| 1510 |
} |
| 1511 |
formset = FormSet(data, instance=repository) |
| 1512 |
self.assertTrue(formset.is_valid()) |
| 1513 |
saved = formset.save() |
| 1514 |
self.assertEqual(len(saved), 1) |
| 1515 |
(revision1,) = saved |
| 1516 |
self.assertEqual(revision1.repository, repository) |
| 1517 |
self.assertEqual(revision1.revision, "146239817507f148d448db38840db7c3cbf47c76") |
| 1518 |
|
| 1519 |
# attempt to save the same revision against the same repo. |
| 1520 |
data = { |
| 1521 |
"revision_set-TOTAL_FORMS": "1", |
| 1522 |
"revision_set-INITIAL_FORMS": "0", |
| 1523 |
"revision_set-MAX_NUM_FORMS": "", |
| 1524 |
"revision_set-0-repository": repository.pk, |
| 1525 |
"revision_set-0-revision": "146239817507f148d448db38840db7c3cbf47c76", |
| 1526 |
"revision_set-0-DELETE": "", |
| 1527 |
} |
| 1528 |
formset = FormSet(data, instance=repository) |
| 1529 |
self.assertFalse(formset.is_valid()) |
| 1530 |
self.assertEqual( |
| 1531 |
formset.errors, |
| 1532 |
[ |
| 1533 |
{ |
| 1534 |
"__all__": [ |
| 1535 |
"Revision with this Repository and Revision already exists." |
| 1536 |
] |
| 1537 |
} |
| 1538 |
], |
| 1539 |
) |
| 1540 |
|
| 1541 |
# unique_together with inlineformset_factory with overridden form fields |
| 1542 |
# Also see #9494 |
| 1543 |
|
| 1544 |
FormSet = inlineformset_factory( |
| 1545 |
Repository, Revision, fields=("revision",), extra=1 |
| 1546 |
) |
| 1547 |
data = { |
| 1548 |
"revision_set-TOTAL_FORMS": "1", |
| 1549 |
"revision_set-INITIAL_FORMS": "0", |
| 1550 |
"revision_set-MAX_NUM_FORMS": "", |
| 1551 |
"revision_set-0-repository": repository.pk, |
| 1552 |
"revision_set-0-revision": "146239817507f148d448db38840db7c3cbf47c76", |
| 1553 |
"revision_set-0-DELETE": "", |
| 1554 |
} |
| 1555 |
formset = FormSet(data, instance=repository) |
| 1556 |
self.assertFalse(formset.is_valid()) |
| 1557 |
|
| 1558 |
def test_callable_defaults(self): |
| 1559 |
# Use of callable defaults (see bug #7975). |
| 1560 |
|
| 1561 |
person = Person.objects.create(name="Ringo") |
| 1562 |
FormSet = inlineformset_factory( |
| 1563 |
Person, Membership, can_delete=False, extra=1, fields="__all__" |
| 1564 |
) |
| 1565 |
formset = FormSet(instance=person) |
| 1566 |
|
| 1567 |
# Django will render a hidden field for model fields that have a callable |
| 1568 |
# default. This is required to ensure the value is tested for change correctly |
| 1569 |
# when determine what extra forms have changed to save. |
| 1570 |
|
| 1571 |
self.assertEqual(len(formset.forms), 1) # this formset only has one form |
| 1572 |
form = formset.forms[0] |
| 1573 |
now = form.fields["date_joined"].initial() |
| 1574 |
result = form.as_p() |
| 1575 |
result = re.sub( |
| 1576 |
r"[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}(?:\.[0-9]+)?", |
| 1577 |
"__DATETIME__", |
| 1578 |
result, |
| 1579 |
) |
| 1580 |
self.assertHTMLEqual( |
| 1581 |
result, |
| 1582 |
'<p><label for="id_membership_set-0-date_joined">Date joined:</label>' |
| 1583 |
'<input type="text" name="membership_set-0-date_joined" ' |
| 1584 |
'value="__DATETIME__" id="id_membership_set-0-date_joined">' |
| 1585 |
'<input type="hidden" name="initial-membership_set-0-date_joined" ' |
| 1586 |
'value="__DATETIME__" ' |
| 1587 |
'id="initial-membership_set-0-id_membership_set-0-date_joined"></p>' |
| 1588 |
'<p><label for="id_membership_set-0-karma">Karma:</label>' |
| 1589 |
'<input type="number" name="membership_set-0-karma" ' |
| 1590 |
'id="id_membership_set-0-karma">' |
| 1591 |
'<input type="hidden" name="membership_set-0-person" value="%d" ' |
| 1592 |
'id="id_membership_set-0-person">' |
| 1593 |
'<input type="hidden" name="membership_set-0-id" ' |
| 1594 |
'id="id_membership_set-0-id"></p>' % person.id, |
| 1595 |
) |
| 1596 |
|
| 1597 |
# test for validation with callable defaults. Validations rely on hidden fields |
| 1598 |
|
| 1599 |
data = { |
| 1600 |
"membership_set-TOTAL_FORMS": "1", |
| 1601 |
"membership_set-INITIAL_FORMS": "0", |
| 1602 |
"membership_set-MAX_NUM_FORMS": "", |
| 1603 |
"membership_set-0-date_joined": now.strftime("%Y-%m-%d %H:%M:%S"), |
| 1604 |
"initial-membership_set-0-date_joined": now.strftime("%Y-%m-%d %H:%M:%S"), |
| 1605 |
"membership_set-0-karma": "", |
| 1606 |
} |
| 1607 |
formset = FormSet(data, instance=person) |
| 1608 |
self.assertTrue(formset.is_valid()) |
| 1609 |
|
| 1610 |
# now test for when the data changes |
| 1611 |
|
| 1612 |
one_day_later = now + datetime.timedelta(days=1) |
| 1613 |
filled_data = { |
| 1614 |
"membership_set-TOTAL_FORMS": "1", |
| 1615 |
"membership_set-INITIAL_FORMS": "0", |
| 1616 |
"membership_set-MAX_NUM_FORMS": "", |
| 1617 |
"membership_set-0-date_joined": one_day_later.strftime("%Y-%m-%d %H:%M:%S"), |
| 1618 |
"initial-membership_set-0-date_joined": now.strftime("%Y-%m-%d %H:%M:%S"), |
| 1619 |
"membership_set-0-karma": "", |
| 1620 |
} |
| 1621 |
formset = FormSet(filled_data, instance=person) |
| 1622 |
self.assertFalse(formset.is_valid()) |
| 1623 |
|
| 1624 |
# now test with split datetime fields |
| 1625 |
|
| 1626 |
class MembershipForm(forms.ModelForm): |
| 1627 |
date_joined = forms.SplitDateTimeField(initial=now) |
| 1628 |
|
| 1629 |
class Meta: |
| 1630 |
model = Membership |
| 1631 |
fields = "__all__" |
| 1632 |
|
| 1633 |
def __init__(self, **kwargs): |
| 1634 |
super().__init__(**kwargs) |
| 1635 |
self.fields["date_joined"].widget = forms.SplitDateTimeWidget() |
| 1636 |
|
| 1637 |
FormSet = inlineformset_factory( |
| 1638 |
Person, |
| 1639 |
Membership, |
| 1640 |
form=MembershipForm, |
| 1641 |
can_delete=False, |
| 1642 |
extra=1, |
| 1643 |
fields="__all__", |
| 1644 |
) |
| 1645 |
data = { |
| 1646 |
"membership_set-TOTAL_FORMS": "1", |
| 1647 |
"membership_set-INITIAL_FORMS": "0", |
| 1648 |
"membership_set-MAX_NUM_FORMS": "", |
| 1649 |
"membership_set-0-date_joined_0": now.strftime("%Y-%m-%d"), |
| 1650 |
"membership_set-0-date_joined_1": now.strftime("%H:%M:%S"), |
| 1651 |
"initial-membership_set-0-date_joined": now.strftime("%Y-%m-%d %H:%M:%S"), |
| 1652 |
"membership_set-0-karma": "", |
| 1653 |
} |
| 1654 |
formset = FormSet(data, instance=person) |
| 1655 |
self.assertTrue(formset.is_valid()) |
| 1656 |
|
| 1657 |
def test_inlineformset_factory_with_null_fk(self): |
| 1658 |
# inlineformset_factory tests with fk having null=True. see #9462. |
| 1659 |
# create some data that will exhibit the issue |
| 1660 |
team = Team.objects.create(name="Red Vipers") |
| 1661 |
Player(name="Timmy").save() |
| 1662 |
Player(name="Bobby", team=team).save() |
| 1663 |
|
| 1664 |
PlayerInlineFormSet = inlineformset_factory(Team, Player, fields="__all__") |
| 1665 |
formset = PlayerInlineFormSet() |
| 1666 |
self.assertQuerySetEqual(formset.get_queryset(), []) |
| 1667 |
|
| 1668 |
formset = PlayerInlineFormSet(instance=team) |
| 1669 |
players = formset.get_queryset() |
| 1670 |
self.assertEqual(len(players), 1) |
| 1671 |
(player1,) = players |
| 1672 |
self.assertEqual(player1.team, team) |
| 1673 |
self.assertEqual(player1.name, "Bobby") |
| 1674 |
|
| 1675 |
def test_inlineformset_with_arrayfield(self): |
| 1676 |
class SimpleArrayField(forms.CharField): |
| 1677 |
"""A proxy for django.contrib.postgres.forms.SimpleArrayField.""" |
| 1678 |
|
| 1679 |
def to_python(self, value): |
| 1680 |
value = super().to_python(value) |
| 1681 |
return value.split(",") if value else [] |
| 1682 |
|
| 1683 |
class BookForm(forms.ModelForm): |
| 1684 |
title = SimpleArrayField() |
| 1685 |
|
| 1686 |
class Meta: |
| 1687 |
model = Book |
| 1688 |
fields = ("title",) |
| 1689 |
|
| 1690 |
BookFormSet = inlineformset_factory(Author, Book, form=BookForm) |
| 1691 |
data = { |
| 1692 |
"book_set-TOTAL_FORMS": "3", |
| 1693 |
"book_set-INITIAL_FORMS": "0", |
| 1694 |
"book_set-MAX_NUM_FORMS": "", |
| 1695 |
"book_set-0-title": "test1,test2", |
| 1696 |
"book_set-1-title": "test1,test2", |
| 1697 |
"book_set-2-title": "test3,test4", |
| 1698 |
} |
| 1699 |
author = Author.objects.create(name="test") |
| 1700 |
formset = BookFormSet(data, instance=author) |
| 1701 |
self.assertEqual( |
| 1702 |
formset.errors, |
| 1703 |
[{}, {"__all__": ["Please correct the duplicate values below."]}, {}], |
| 1704 |
) |
| 1705 |
|
| 1706 |
def test_inlineformset_with_jsonfield(self): |
| 1707 |
class BookForm(forms.ModelForm): |
| 1708 |
title = forms.JSONField() |
| 1709 |
|
| 1710 |
class Meta: |
| 1711 |
model = Book |
| 1712 |
fields = ("title",) |
| 1713 |
|
| 1714 |
BookFormSet = inlineformset_factory(Author, Book, form=BookForm) |
| 1715 |
data = { |
| 1716 |
"book_set-TOTAL_FORMS": "3", |
| 1717 |
"book_set-INITIAL_FORMS": "0", |
| 1718 |
"book_set-MAX_NUM_FORMS": "", |
| 1719 |
"book_set-0-title": {"test1": "test2"}, |
| 1720 |
"book_set-1-title": {"test1": "test2"}, |
| 1721 |
"book_set-2-title": {"test3": "test4"}, |
| 1722 |
} |
| 1723 |
author = Author.objects.create(name="test") |
| 1724 |
formset = BookFormSet(data, instance=author) |
| 1725 |
self.assertEqual( |
| 1726 |
formset.errors, |
| 1727 |
[{}, {"__all__": ["Please correct the duplicate values below."]}, {}], |
| 1728 |
) |
| 1729 |
|
| 1730 |
def test_model_formset_with_custom_pk(self): |
| 1731 |
# a formset for a Model that has a custom primary key that still needs to be |
| 1732 |
# added to the formset automatically |
| 1733 |
FormSet = modelformset_factory( |
| 1734 |
ClassyMexicanRestaurant, fields=["tacos_are_yummy"] |
| 1735 |
) |
| 1736 |
self.assertEqual( |
| 1737 |
sorted(FormSet().forms[0].fields), ["tacos_are_yummy", "the_restaurant"] |
| 1738 |
) |
| 1739 |
|
| 1740 |
def test_model_formset_with_initial_model_instance(self): |
| 1741 |
# has_changed should compare model instance and primary key |
| 1742 |
# see #18898 |
| 1743 |
FormSet = modelformset_factory(Poem, fields="__all__") |
| 1744 |
john_milton = Poet(name="John Milton") |
| 1745 |
john_milton.save() |
| 1746 |
data = { |
| 1747 |
"form-TOTAL_FORMS": 1, |
| 1748 |
"form-INITIAL_FORMS": 0, |
| 1749 |
"form-MAX_NUM_FORMS": "", |
| 1750 |
"form-0-name": "", |
| 1751 |
"form-0-poet": str(john_milton.id), |
| 1752 |
} |
| 1753 |
formset = FormSet(initial=[{"poet": john_milton}], data=data) |
| 1754 |
self.assertFalse(formset.extra_forms[0].has_changed()) |
| 1755 |
|
| 1756 |
def test_model_formset_with_initial_queryset(self): |
| 1757 |
# has_changed should work with queryset and list of pk's |
| 1758 |
# see #18898 |
| 1759 |
FormSet = modelformset_factory(AuthorMeeting, fields="__all__") |
| 1760 |
Author.objects.create(pk=1, name="Charles Baudelaire") |
| 1761 |
data = { |
| 1762 |
"form-TOTAL_FORMS": 1, |
| 1763 |
"form-INITIAL_FORMS": 0, |
| 1764 |
"form-MAX_NUM_FORMS": "", |
| 1765 |
"form-0-name": "", |
| 1766 |
"form-0-created": "", |
| 1767 |
"form-0-authors": list(Author.objects.values_list("id", flat=True)), |
| 1768 |
} |
| 1769 |
formset = FormSet(initial=[{"authors": Author.objects.all()}], data=data) |
| 1770 |
self.assertFalse(formset.extra_forms[0].has_changed()) |
| 1771 |
|
| 1772 |
def test_prevent_duplicates_from_with_the_same_formset(self): |
| 1773 |
FormSet = modelformset_factory(Product, fields="__all__", extra=2) |
| 1774 |
data = { |
| 1775 |
"form-TOTAL_FORMS": 2, |
| 1776 |
"form-INITIAL_FORMS": 0, |
| 1777 |
"form-MAX_NUM_FORMS": "", |
| 1778 |
"form-0-slug": "red_car", |
| 1779 |
"form-1-slug": "red_car", |
| 1780 |
} |
| 1781 |
formset = FormSet(data) |
| 1782 |
self.assertFalse(formset.is_valid()) |
| 1783 |
self.assertEqual( |
| 1784 |
formset._non_form_errors, ["Please correct the duplicate data for slug."] |
| 1785 |
) |
| 1786 |
|
| 1787 |
FormSet = modelformset_factory(Price, fields="__all__", extra=2) |
| 1788 |
data = { |
| 1789 |
"form-TOTAL_FORMS": 2, |
| 1790 |
"form-INITIAL_FORMS": 0, |
| 1791 |
"form-MAX_NUM_FORMS": "", |
| 1792 |
"form-0-price": "25", |
| 1793 |
"form-0-quantity": "7", |
| 1794 |
"form-1-price": "25", |
| 1795 |
"form-1-quantity": "7", |
| 1796 |
} |
| 1797 |
formset = FormSet(data) |
| 1798 |
self.assertFalse(formset.is_valid()) |
| 1799 |
self.assertEqual( |
| 1800 |
formset._non_form_errors, |
| 1801 |
[ |
| 1802 |
"Please correct the duplicate data for price and quantity, which must " |
| 1803 |
"be unique." |
| 1804 |
], |
| 1805 |
) |
| 1806 |
|
| 1807 |
# Only the price field is specified, this should skip any unique |
| 1808 |
# checks since the unique_together is not fulfilled. This will fail |
| 1809 |
# with a KeyError if broken. |
| 1810 |
FormSet = modelformset_factory(Price, fields=("price",), extra=2) |
| 1811 |
data = { |
| 1812 |
"form-TOTAL_FORMS": "2", |
| 1813 |
"form-INITIAL_FORMS": "0", |
| 1814 |
"form-MAX_NUM_FORMS": "", |
| 1815 |
"form-0-price": "24", |
| 1816 |
"form-1-price": "24", |
| 1817 |
} |
| 1818 |
formset = FormSet(data) |
| 1819 |
self.assertTrue(formset.is_valid()) |
| 1820 |
|
| 1821 |
FormSet = inlineformset_factory(Author, Book, extra=0, fields="__all__") |
| 1822 |
author = Author.objects.create(pk=1, name="Charles Baudelaire") |
| 1823 |
Book.objects.create(pk=1, author=author, title="Les Paradis Artificiels") |
| 1824 |
Book.objects.create(pk=2, author=author, title="Les Fleurs du Mal") |
| 1825 |
Book.objects.create(pk=3, author=author, title="Flowers of Evil") |
| 1826 |
|
| 1827 |
book_ids = author.book_set.order_by("id").values_list("id", flat=True) |
| 1828 |
data = { |
| 1829 |
"book_set-TOTAL_FORMS": "2", |
| 1830 |
"book_set-INITIAL_FORMS": "2", |
| 1831 |
"book_set-MAX_NUM_FORMS": "", |
| 1832 |
"book_set-0-title": "The 2008 Election", |
| 1833 |
"book_set-0-author": str(author.id), |
| 1834 |
"book_set-0-id": str(book_ids[0]), |
| 1835 |
"book_set-1-title": "The 2008 Election", |
| 1836 |
"book_set-1-author": str(author.id), |
| 1837 |
"book_set-1-id": str(book_ids[1]), |
| 1838 |
} |
| 1839 |
formset = FormSet(data=data, instance=author) |
| 1840 |
self.assertFalse(formset.is_valid()) |
| 1841 |
self.assertEqual( |
| 1842 |
formset._non_form_errors, ["Please correct the duplicate data for title."] |
| 1843 |
) |
| 1844 |
self.assertEqual( |
| 1845 |
formset.errors, |
| 1846 |
[{}, {"__all__": ["Please correct the duplicate values below."]}], |
| 1847 |
) |
| 1848 |
|
| 1849 |
FormSet = modelformset_factory(Post, fields="__all__", extra=2) |
| 1850 |
data = { |
| 1851 |
"form-TOTAL_FORMS": "2", |
| 1852 |
"form-INITIAL_FORMS": "0", |
| 1853 |
"form-MAX_NUM_FORMS": "", |
| 1854 |
"form-0-title": "blah", |
| 1855 |
"form-0-slug": "Morning", |
| 1856 |
"form-0-subtitle": "foo", |
| 1857 |
"form-0-posted": "2009-01-01", |
| 1858 |
"form-1-title": "blah", |
| 1859 |
"form-1-slug": "Morning in Prague", |
| 1860 |
"form-1-subtitle": "rawr", |
| 1861 |
"form-1-posted": "2009-01-01", |
| 1862 |
} |
| 1863 |
formset = FormSet(data) |
| 1864 |
self.assertFalse(formset.is_valid()) |
| 1865 |
self.assertEqual( |
| 1866 |
formset._non_form_errors, |
| 1867 |
[ |
| 1868 |
"Please correct the duplicate data for title which must be unique for " |
| 1869 |
"the date in posted." |
| 1870 |
], |
| 1871 |
) |
| 1872 |
self.assertEqual( |
| 1873 |
formset.errors, |
| 1874 |
[{}, {"__all__": ["Please correct the duplicate values below."]}], |
| 1875 |
) |
| 1876 |
|
| 1877 |
data = { |
| 1878 |
"form-TOTAL_FORMS": "2", |
| 1879 |
"form-INITIAL_FORMS": "0", |
| 1880 |
"form-MAX_NUM_FORMS": "", |
| 1881 |
"form-0-title": "foo", |
| 1882 |
"form-0-slug": "Morning in Prague", |
| 1883 |
"form-0-subtitle": "foo", |
| 1884 |
"form-0-posted": "2009-01-01", |
| 1885 |
"form-1-title": "blah", |
| 1886 |
"form-1-slug": "Morning in Prague", |
| 1887 |
"form-1-subtitle": "rawr", |
| 1888 |
"form-1-posted": "2009-08-02", |
| 1889 |
} |
| 1890 |
formset = FormSet(data) |
| 1891 |
self.assertFalse(formset.is_valid()) |
| 1892 |
self.assertEqual( |
| 1893 |
formset._non_form_errors, |
| 1894 |
[ |
| 1895 |
"Please correct the duplicate data for slug which must be unique for " |
| 1896 |
"the year in posted." |
| 1897 |
], |
| 1898 |
) |
| 1899 |
|
| 1900 |
data = { |
| 1901 |
"form-TOTAL_FORMS": "2", |
| 1902 |
"form-INITIAL_FORMS": "0", |
| 1903 |
"form-MAX_NUM_FORMS": "", |
| 1904 |
"form-0-title": "foo", |
| 1905 |
"form-0-slug": "Morning in Prague", |
| 1906 |
"form-0-subtitle": "rawr", |
| 1907 |
"form-0-posted": "2008-08-01", |
| 1908 |
"form-1-title": "blah", |
| 1909 |
"form-1-slug": "Prague", |
| 1910 |
"form-1-subtitle": "rawr", |
| 1911 |
"form-1-posted": "2009-08-02", |
| 1912 |
} |
| 1913 |
formset = FormSet(data) |
| 1914 |
self.assertFalse(formset.is_valid()) |
| 1915 |
self.assertEqual( |
| 1916 |
formset._non_form_errors, |
| 1917 |
[ |
| 1918 |
"Please correct the duplicate data for subtitle which must be unique " |
| 1919 |
"for the month in posted." |
| 1920 |
], |
| 1921 |
) |
| 1922 |
|
| 1923 |
def test_prevent_change_outer_model_and_create_invalid_data(self): |
| 1924 |
author = Author.objects.create(name="Charles") |
| 1925 |
other_author = Author.objects.create(name="Walt") |
| 1926 |
AuthorFormSet = modelformset_factory(Author, fields="__all__") |
| 1927 |
data = { |
| 1928 |
"form-TOTAL_FORMS": "2", |
| 1929 |
"form-INITIAL_FORMS": "2", |
| 1930 |
"form-MAX_NUM_FORMS": "", |
| 1931 |
"form-0-id": str(author.id), |
| 1932 |
"form-0-name": "Charles", |
| 1933 |
"form-1-id": str(other_author.id), # A model not in the formset's queryset. |
| 1934 |
"form-1-name": "Changed name", |
| 1935 |
} |
| 1936 |
# This formset is only for Walt Whitman and shouldn't accept data for |
| 1937 |
# other_author. |
| 1938 |
formset = AuthorFormSet( |
| 1939 |
data=data, queryset=Author.objects.filter(id__in=(author.id,)) |
| 1940 |
) |
| 1941 |
self.assertTrue(formset.is_valid()) |
| 1942 |
formset.save() |
| 1943 |
# The name of other_author shouldn't be changed and new models aren't |
| 1944 |
# created. |
| 1945 |
self.assertSequenceEqual(Author.objects.all(), [author, other_author]) |
| 1946 |
|
| 1947 |
def test_validation_without_id(self): |
| 1948 |
AuthorFormSet = modelformset_factory(Author, fields="__all__") |
| 1949 |
data = { |
| 1950 |
"form-TOTAL_FORMS": "1", |
| 1951 |
"form-INITIAL_FORMS": "1", |
| 1952 |
"form-MAX_NUM_FORMS": "", |
| 1953 |
"form-0-name": "Charles", |
| 1954 |
} |
| 1955 |
formset = AuthorFormSet(data) |
| 1956 |
self.assertEqual( |
| 1957 |
formset.errors, |
| 1958 |
[{"id": ["This field is required."]}], |
| 1959 |
) |
| 1960 |
|
| 1961 |
def test_validation_with_child_model_without_id(self): |
| 1962 |
BetterAuthorFormSet = modelformset_factory(BetterAuthor, fields="__all__") |
| 1963 |
data = { |
| 1964 |
"form-TOTAL_FORMS": "1", |
| 1965 |
"form-INITIAL_FORMS": "1", |
| 1966 |
"form-MAX_NUM_FORMS": "", |
| 1967 |
"form-0-name": "Charles", |
| 1968 |
"form-0-write_speed": "10", |
| 1969 |
} |
| 1970 |
formset = BetterAuthorFormSet(data) |
| 1971 |
self.assertEqual( |
| 1972 |
formset.errors, |
| 1973 |
[{"author_ptr": ["This field is required."]}], |
| 1974 |
) |
| 1975 |
|
| 1976 |
def test_validation_with_invalid_id(self): |
| 1977 |
AuthorFormSet = modelformset_factory(Author, fields="__all__") |
| 1978 |
data = { |
| 1979 |
"form-TOTAL_FORMS": "1", |
| 1980 |
"form-INITIAL_FORMS": "1", |
| 1981 |
"form-MAX_NUM_FORMS": "", |
| 1982 |
"form-0-id": "abc", |
| 1983 |
"form-0-name": "Charles", |
| 1984 |
} |
| 1985 |
formset = AuthorFormSet(data) |
| 1986 |
self.assertEqual( |
| 1987 |
formset.errors, |
| 1988 |
[ |
| 1989 |
{ |
| 1990 |
"id": [ |
| 1991 |
"Select a valid choice. That choice is not one of the " |
| 1992 |
"available choices." |
| 1993 |
] |
| 1994 |
} |
| 1995 |
], |
| 1996 |
) |
| 1997 |
|
| 1998 |
def test_validation_with_nonexistent_id(self): |
| 1999 |
AuthorFormSet = modelformset_factory(Author, fields="__all__") |
| 2000 |
data = { |
| 2001 |
"form-TOTAL_FORMS": "1", |
| 2002 |
"form-INITIAL_FORMS": "1", |
| 2003 |
"form-MAX_NUM_FORMS": "", |
| 2004 |
"form-0-id": "12345", |
| 2005 |
"form-0-name": "Charles", |
| 2006 |
} |
| 2007 |
formset = AuthorFormSet(data) |
| 2008 |
self.assertEqual( |
| 2009 |
formset.errors, |
| 2010 |
[ |
| 2011 |
{ |
| 2012 |
"id": [ |
| 2013 |
"Select a valid choice. That choice is not one of the " |
| 2014 |
"available choices." |
| 2015 |
] |
| 2016 |
} |
| 2017 |
], |
| 2018 |
) |
| 2019 |
|
| 2020 |
def test_initial_form_count_empty_data(self): |
| 2021 |
AuthorFormSet = modelformset_factory(Author, fields="__all__") |
| 2022 |
formset = AuthorFormSet({}) |
| 2023 |
self.assertEqual(formset.initial_form_count(), 0) |
| 2024 |
|
| 2025 |
def test_edit_only(self): |
| 2026 |
charles = Author.objects.create(name="Charles Baudelaire") |
| 2027 |
AuthorFormSet = modelformset_factory(Author, fields="__all__", edit_only=True) |
| 2028 |
data = { |
| 2029 |
"form-TOTAL_FORMS": "2", |
| 2030 |
"form-INITIAL_FORMS": "0", |
| 2031 |
"form-MAX_NUM_FORMS": "0", |
| 2032 |
"form-0-name": "Arthur Rimbaud", |
| 2033 |
"form-1-name": "Walt Whitman", |
| 2034 |
} |
| 2035 |
formset = AuthorFormSet(data) |
| 2036 |
self.assertIs(formset.is_valid(), True) |
| 2037 |
formset.save() |
| 2038 |
self.assertSequenceEqual(Author.objects.all(), [charles]) |
| 2039 |
data = { |
| 2040 |
"form-TOTAL_FORMS": "2", |
| 2041 |
"form-INITIAL_FORMS": "1", |
| 2042 |
"form-MAX_NUM_FORMS": "0", |
| 2043 |
"form-0-id": charles.pk, |
| 2044 |
"form-0-name": "Arthur Rimbaud", |
| 2045 |
"form-1-name": "Walt Whitman", |
| 2046 |
} |
| 2047 |
formset = AuthorFormSet(data) |
| 2048 |
self.assertIs(formset.is_valid(), True) |
| 2049 |
formset.save() |
| 2050 |
charles.refresh_from_db() |
| 2051 |
self.assertEqual(charles.name, "Arthur Rimbaud") |
| 2052 |
self.assertSequenceEqual(Author.objects.all(), [charles]) |
| 2053 |
|
| 2054 |
def test_edit_only_inlineformset_factory(self): |
| 2055 |
charles = Author.objects.create(name="Charles Baudelaire") |
| 2056 |
book = Book.objects.create(author=charles, title="Les Paradis Artificiels") |
| 2057 |
AuthorFormSet = inlineformset_factory( |
| 2058 |
Author, |
| 2059 |
Book, |
| 2060 |
can_delete=False, |
| 2061 |
fields="__all__", |
| 2062 |
edit_only=True, |
| 2063 |
) |
| 2064 |
data = { |
| 2065 |
"book_set-TOTAL_FORMS": "4", |
| 2066 |
"book_set-INITIAL_FORMS": "1", |
| 2067 |
"book_set-MAX_NUM_FORMS": "0", |
| 2068 |
"book_set-0-id": book.pk, |
| 2069 |
"book_set-0-title": "Les Fleurs du Mal", |
| 2070 |
"book_set-0-author": charles.pk, |
| 2071 |
"book_set-1-title": "Flowers of Evil", |
| 2072 |
"book_set-1-author": charles.pk, |
| 2073 |
} |
| 2074 |
formset = AuthorFormSet(data, instance=charles) |
| 2075 |
self.assertIs(formset.is_valid(), True) |
| 2076 |
formset.save() |
| 2077 |
book.refresh_from_db() |
| 2078 |
self.assertEqual(book.title, "Les Fleurs du Mal") |
| 2079 |
self.assertSequenceEqual(Book.objects.all(), [book]) |
| 2080 |
|
| 2081 |
def test_edit_only_object_outside_of_queryset(self): |
| 2082 |
charles = Author.objects.create(name="Charles Baudelaire") |
| 2083 |
walt = Author.objects.create(name="Walt Whitman") |
| 2084 |
data = { |
| 2085 |
"form-TOTAL_FORMS": "1", |
| 2086 |
"form-INITIAL_FORMS": "1", |
| 2087 |
"form-0-id": walt.pk, |
| 2088 |
"form-0-name": "Parth Patil", |
| 2089 |
} |
| 2090 |
AuthorFormSet = modelformset_factory(Author, fields="__all__", edit_only=True) |
| 2091 |
formset = AuthorFormSet(data, queryset=Author.objects.filter(pk=charles.pk)) |
| 2092 |
self.assertIs(formset.is_valid(), True) |
| 2093 |
formset.save() |
| 2094 |
self.assertCountEqual(Author.objects.all(), [charles, walt]) |
| 2095 |
|
| 2096 |
def test_edit_only_formset_factory_with_basemodelformset(self): |
| 2097 |
charles = Author.objects.create(name="Charles Baudelaire") |
| 2098 |
|
| 2099 |
class AuthorForm(forms.ModelForm): |
| 2100 |
class Meta: |
| 2101 |
model = Author |
| 2102 |
fields = "__all__" |
| 2103 |
|
| 2104 |
class BaseAuthorFormSet(BaseModelFormSet): |
| 2105 |
def __init__(self, *args, **kwargs): |
| 2106 |
self.model = Author |
| 2107 |
super().__init__(*args, **kwargs) |
| 2108 |
|
| 2109 |
AuthorFormSet = formset_factory(AuthorForm, formset=BaseAuthorFormSet) |
| 2110 |
data = { |
| 2111 |
"form-TOTAL_FORMS": "2", |
| 2112 |
"form-INITIAL_FORMS": "1", |
| 2113 |
"form-MAX_NUM_FORMS": "0", |
| 2114 |
"form-0-id": charles.pk, |
| 2115 |
"form-0-name": "Shawn Dong", |
| 2116 |
"form-1-name": "Walt Whitman", |
| 2117 |
} |
| 2118 |
formset = AuthorFormSet(data) |
| 2119 |
self.assertIs(formset.is_valid(), True) |
| 2120 |
formset.save() |
| 2121 |
self.assertEqual(Author.objects.count(), 2) |
| 2122 |
charles.refresh_from_db() |
| 2123 |
self.assertEqual(charles.name, "Shawn Dong") |
| 2124 |
self.assertEqual(Author.objects.count(), 2) |
| 2125 |
|
| 2126 |
|
| 2127 |
class TestModelFormsetOverridesTroughFormMeta(TestCase): |
| 2128 |
def test_modelformset_factory_widgets(self): |
| 2129 |
widgets = {"name": forms.TextInput(attrs={"class": "poet"})} |
| 2130 |
PoetFormSet = modelformset_factory(Poet, fields="__all__", widgets=widgets) |
| 2131 |
form = PoetFormSet.form() |
| 2132 |
self.assertHTMLEqual( |
| 2133 |
str(form["name"]), |
| 2134 |
'<input id="id_name" maxlength="100" type="text" class="poet" name="name" ' |
| 2135 |
"required>", |
| 2136 |
) |
| 2137 |
|
| 2138 |
def test_inlineformset_factory_widgets(self): |
| 2139 |
widgets = {"title": forms.TextInput(attrs={"class": "book"})} |
| 2140 |
BookFormSet = inlineformset_factory( |
| 2141 |
Author, Book, widgets=widgets, fields="__all__" |
| 2142 |
) |
| 2143 |
form = BookFormSet.form() |
| 2144 |
self.assertHTMLEqual( |
| 2145 |
str(form["title"]), |
| 2146 |
'<input class="book" id="id_title" maxlength="100" name="title" ' |
| 2147 |
'type="text" required>', |
| 2148 |
) |
| 2149 |
|
| 2150 |
def test_modelformset_factory_labels_overrides(self): |
| 2151 |
BookFormSet = modelformset_factory( |
| 2152 |
Book, fields="__all__", labels={"title": "Name"} |
| 2153 |
) |
| 2154 |
form = BookFormSet.form() |
| 2155 |
self.assertHTMLEqual( |
| 2156 |
form["title"].label_tag(), '<label for="id_title">Name:</label>' |
| 2157 |
) |
| 2158 |
self.assertHTMLEqual( |
| 2159 |
form["title"].legend_tag(), |
| 2160 |
'<legend for="id_title">Name:</legend>', |
| 2161 |
) |
| 2162 |
|
| 2163 |
def test_inlineformset_factory_labels_overrides(self): |
| 2164 |
BookFormSet = inlineformset_factory( |
| 2165 |
Author, Book, fields="__all__", labels={"title": "Name"} |
| 2166 |
) |
| 2167 |
form = BookFormSet.form() |
| 2168 |
self.assertHTMLEqual( |
| 2169 |
form["title"].label_tag(), '<label for="id_title">Name:</label>' |
| 2170 |
) |
| 2171 |
self.assertHTMLEqual( |
| 2172 |
form["title"].legend_tag(), |
| 2173 |
'<legend for="id_title">Name:</legend>', |
| 2174 |
) |
| 2175 |
|
| 2176 |
def test_modelformset_factory_help_text_overrides(self): |
| 2177 |
BookFormSet = modelformset_factory( |
| 2178 |
Book, fields="__all__", help_texts={"title": "Choose carefully."} |
| 2179 |
) |
| 2180 |
form = BookFormSet.form() |
| 2181 |
self.assertEqual(form["title"].help_text, "Choose carefully.") |
| 2182 |
|
| 2183 |
def test_inlineformset_factory_help_text_overrides(self): |
| 2184 |
BookFormSet = inlineformset_factory( |
| 2185 |
Author, Book, fields="__all__", help_texts={"title": "Choose carefully."} |
| 2186 |
) |
| 2187 |
form = BookFormSet.form() |
| 2188 |
self.assertEqual(form["title"].help_text, "Choose carefully.") |
| 2189 |
|
| 2190 |
def test_modelformset_factory_error_messages_overrides(self): |
| 2191 |
author = Author.objects.create(pk=1, name="Charles Baudelaire") |
| 2192 |
BookFormSet = modelformset_factory( |
| 2193 |
Book, |
| 2194 |
fields="__all__", |
| 2195 |
error_messages={"title": {"max_length": "Title too long!!"}}, |
| 2196 |
) |
| 2197 |
form = BookFormSet.form(data={"title": "Foo " * 30, "author": author.id}) |
| 2198 |
form.full_clean() |
| 2199 |
self.assertEqual(form.errors, {"title": ["Title too long!!"]}) |
| 2200 |
|
| 2201 |
def test_inlineformset_factory_error_messages_overrides(self): |
| 2202 |
author = Author.objects.create(pk=1, name="Charles Baudelaire") |
| 2203 |
BookFormSet = inlineformset_factory( |
| 2204 |
Author, |
| 2205 |
Book, |
| 2206 |
fields="__all__", |
| 2207 |
error_messages={"title": {"max_length": "Title too long!!"}}, |
| 2208 |
) |
| 2209 |
form = BookFormSet.form(data={"title": "Foo " * 30, "author": author.id}) |
| 2210 |
form.full_clean() |
| 2211 |
self.assertEqual(form.errors, {"title": ["Title too long!!"]}) |
| 2212 |
|
| 2213 |
def test_modelformset_factory_field_class_overrides(self): |
| 2214 |
author = Author.objects.create(pk=1, name="Charles Baudelaire") |
| 2215 |
BookFormSet = modelformset_factory( |
| 2216 |
Book, |
| 2217 |
fields="__all__", |
| 2218 |
field_classes={ |
| 2219 |
"title": forms.SlugField, |
| 2220 |
}, |
| 2221 |
) |
| 2222 |
form = BookFormSet.form(data={"title": "Foo " * 30, "author": author.id}) |
| 2223 |
self.assertIs(Book._meta.get_field("title").__class__, models.CharField) |
| 2224 |
self.assertIsInstance(form.fields["title"], forms.SlugField) |
| 2225 |
|
| 2226 |
def test_inlineformset_factory_field_class_overrides(self): |
| 2227 |
author = Author.objects.create(pk=1, name="Charles Baudelaire") |
| 2228 |
BookFormSet = inlineformset_factory( |
| 2229 |
Author, |
| 2230 |
Book, |
| 2231 |
fields="__all__", |
| 2232 |
field_classes={ |
| 2233 |
"title": forms.SlugField, |
| 2234 |
}, |
| 2235 |
) |
| 2236 |
form = BookFormSet.form(data={"title": "Foo " * 30, "author": author.id}) |
| 2237 |
self.assertIs(Book._meta.get_field("title").__class__, models.CharField) |
| 2238 |
self.assertIsInstance(form.fields["title"], forms.SlugField) |
| 2239 |
|
| 2240 |
def test_modelformset_factory_absolute_max(self): |
| 2241 |
AuthorFormSet = modelformset_factory( |
| 2242 |
Author, fields="__all__", absolute_max=1500 |
| 2243 |
) |
| 2244 |
data = { |
| 2245 |
"form-TOTAL_FORMS": "1501", |
| 2246 |
"form-INITIAL_FORMS": "0", |
| 2247 |
"form-MAX_NUM_FORMS": "0", |
| 2248 |
} |
| 2249 |
formset = AuthorFormSet(data=data) |
| 2250 |
self.assertIs(formset.is_valid(), False) |
| 2251 |
self.assertEqual(len(formset.forms), 1500) |
| 2252 |
self.assertEqual( |
| 2253 |
formset.non_form_errors(), |
| 2254 |
["Please submit at most 1000 forms."], |
| 2255 |
) |
| 2256 |
|
| 2257 |
def test_modelformset_factory_absolute_max_with_max_num(self): |
| 2258 |
AuthorFormSet = modelformset_factory( |
| 2259 |
Author, |
| 2260 |
fields="__all__", |
| 2261 |
max_num=20, |
| 2262 |
absolute_max=100, |
| 2263 |
) |
| 2264 |
data = { |
| 2265 |
"form-TOTAL_FORMS": "101", |
| 2266 |
"form-INITIAL_FORMS": "0", |
| 2267 |
"form-MAX_NUM_FORMS": "0", |
| 2268 |
} |
| 2269 |
formset = AuthorFormSet(data=data) |
| 2270 |
self.assertIs(formset.is_valid(), False) |
| 2271 |
self.assertEqual(len(formset.forms), 100) |
| 2272 |
self.assertEqual( |
| 2273 |
formset.non_form_errors(), |
| 2274 |
["Please submit at most 20 forms."], |
| 2275 |
) |
| 2276 |
|
| 2277 |
def test_inlineformset_factory_absolute_max(self): |
| 2278 |
author = Author.objects.create(name="Charles Baudelaire") |
| 2279 |
BookFormSet = inlineformset_factory( |
| 2280 |
Author, |
| 2281 |
Book, |
| 2282 |
fields="__all__", |
| 2283 |
absolute_max=1500, |
| 2284 |
) |
| 2285 |
data = { |
| 2286 |
"book_set-TOTAL_FORMS": "1501", |
| 2287 |
"book_set-INITIAL_FORMS": "0", |
| 2288 |
"book_set-MAX_NUM_FORMS": "0", |
| 2289 |
} |
| 2290 |
formset = BookFormSet(data, instance=author) |
| 2291 |
self.assertIs(formset.is_valid(), False) |
| 2292 |
self.assertEqual(len(formset.forms), 1500) |
| 2293 |
self.assertEqual( |
| 2294 |
formset.non_form_errors(), |
| 2295 |
["Please submit at most 1000 forms."], |
| 2296 |
) |
| 2297 |
|
| 2298 |
def test_inlineformset_factory_absolute_max_with_max_num(self): |
| 2299 |
author = Author.objects.create(name="Charles Baudelaire") |
| 2300 |
BookFormSet = inlineformset_factory( |
| 2301 |
Author, |
| 2302 |
Book, |
| 2303 |
fields="__all__", |
| 2304 |
max_num=20, |
| 2305 |
absolute_max=100, |
| 2306 |
) |
| 2307 |
data = { |
| 2308 |
"book_set-TOTAL_FORMS": "101", |
| 2309 |
"book_set-INITIAL_FORMS": "0", |
| 2310 |
"book_set-MAX_NUM_FORMS": "0", |
| 2311 |
} |
| 2312 |
formset = BookFormSet(data, instance=author) |
| 2313 |
self.assertIs(formset.is_valid(), False) |
| 2314 |
self.assertEqual(len(formset.forms), 100) |
| 2315 |
self.assertEqual( |
| 2316 |
formset.non_form_errors(), |
| 2317 |
["Please submit at most 20 forms."], |
| 2318 |
) |
| 2319 |
|
| 2320 |
def test_modelformset_factory_can_delete_extra(self): |
| 2321 |
AuthorFormSet = modelformset_factory( |
| 2322 |
Author, |
| 2323 |
fields="__all__", |
| 2324 |
can_delete=True, |
| 2325 |
can_delete_extra=True, |
| 2326 |
extra=2, |
| 2327 |
) |
| 2328 |
formset = AuthorFormSet() |
| 2329 |
self.assertEqual(len(formset), 2) |
| 2330 |
self.assertIn("DELETE", formset.forms[0].fields) |
| 2331 |
self.assertIn("DELETE", formset.forms[1].fields) |
| 2332 |
|
| 2333 |
def test_modelformset_factory_disable_delete_extra(self): |
| 2334 |
AuthorFormSet = modelformset_factory( |
| 2335 |
Author, |
| 2336 |
fields="__all__", |
| 2337 |
can_delete=True, |
| 2338 |
can_delete_extra=False, |
| 2339 |
extra=2, |
| 2340 |
) |
| 2341 |
formset = AuthorFormSet() |
| 2342 |
self.assertEqual(len(formset), 2) |
| 2343 |
self.assertNotIn("DELETE", formset.forms[0].fields) |
| 2344 |
self.assertNotIn("DELETE", formset.forms[1].fields) |
| 2345 |
|
| 2346 |
def test_inlineformset_factory_can_delete_extra(self): |
| 2347 |
BookFormSet = inlineformset_factory( |
| 2348 |
Author, |
| 2349 |
Book, |
| 2350 |
fields="__all__", |
| 2351 |
can_delete=True, |
| 2352 |
can_delete_extra=True, |
| 2353 |
extra=2, |
| 2354 |
) |
| 2355 |
formset = BookFormSet() |
| 2356 |
self.assertEqual(len(formset), 2) |
| 2357 |
self.assertIn("DELETE", formset.forms[0].fields) |
| 2358 |
self.assertIn("DELETE", formset.forms[1].fields) |
| 2359 |
|
| 2360 |
def test_inlineformset_factory_can_not_delete_extra(self): |
| 2361 |
BookFormSet = inlineformset_factory( |
| 2362 |
Author, |
| 2363 |
Book, |
| 2364 |
fields="__all__", |
| 2365 |
can_delete=True, |
| 2366 |
can_delete_extra=False, |
| 2367 |
extra=2, |
| 2368 |
) |
| 2369 |
formset = BookFormSet() |
| 2370 |
self.assertEqual(len(formset), 2) |
| 2371 |
self.assertNotIn("DELETE", formset.forms[0].fields) |
| 2372 |
self.assertNotIn("DELETE", formset.forms[1].fields) |
| 2373 |
|
| 2374 |
def test_inlineformset_factory_passes_renderer(self): |
| 2375 |
from django.forms.renderers import Jinja2 |
| 2376 |
|
| 2377 |
renderer = Jinja2() |
| 2378 |
BookFormSet = inlineformset_factory( |
| 2379 |
Author, |
| 2380 |
Book, |
| 2381 |
fields="__all__", |
| 2382 |
renderer=renderer, |
| 2383 |
) |
| 2384 |
formset = BookFormSet() |
| 2385 |
self.assertEqual(formset.renderer, renderer) |
| 2386 |
|
| 2387 |
def test_modelformset_factory_passes_renderer(self): |
| 2388 |
from django.forms.renderers import Jinja2 |
| 2389 |
|
| 2390 |
renderer = Jinja2() |
| 2391 |
BookFormSet = modelformset_factory(Author, fields="__all__", renderer=renderer) |
| 2392 |
formset = BookFormSet() |
| 2393 |
self.assertEqual(formset.renderer, renderer) |
| 2394 |
|
| 2395 |
def test_modelformset_factory_default_renderer(self): |
| 2396 |
class CustomRenderer(DjangoTemplates): |
| 2397 |
pass |
| 2398 |
|
| 2399 |
class ModelFormWithDefaultRenderer(ModelForm): |
| 2400 |
default_renderer = CustomRenderer() |
| 2401 |
|
| 2402 |
BookFormSet = modelformset_factory( |
| 2403 |
Author, form=ModelFormWithDefaultRenderer, fields="__all__" |
| 2404 |
) |
| 2405 |
formset = BookFormSet() |
| 2406 |
self.assertEqual( |
| 2407 |
formset.forms[0].renderer, ModelFormWithDefaultRenderer.default_renderer |
| 2408 |
) |
| 2409 |
self.assertEqual( |
| 2410 |
formset.empty_form.renderer, ModelFormWithDefaultRenderer.default_renderer |
| 2411 |
) |
| 2412 |
self.assertIsInstance(formset.renderer, DjangoTemplates) |
| 2413 |
|
| 2414 |
def test_inlineformset_factory_default_renderer(self): |
| 2415 |
class CustomRenderer(DjangoTemplates): |
| 2416 |
pass |
| 2417 |
|
| 2418 |
class ModelFormWithDefaultRenderer(ModelForm): |
| 2419 |
default_renderer = CustomRenderer() |
| 2420 |
|
| 2421 |
BookFormSet = inlineformset_factory( |
| 2422 |
Author, |
| 2423 |
Book, |
| 2424 |
form=ModelFormWithDefaultRenderer, |
| 2425 |
fields="__all__", |
| 2426 |
) |
| 2427 |
formset = BookFormSet() |
| 2428 |
self.assertEqual( |
| 2429 |
formset.forms[0].renderer, ModelFormWithDefaultRenderer.default_renderer |
| 2430 |
) |
| 2431 |
self.assertEqual( |
| 2432 |
formset.empty_form.renderer, ModelFormWithDefaultRenderer.default_renderer |
| 2433 |
) |
| 2434 |
self.assertIsInstance(formset.renderer, DjangoTemplates) |