2010-09-11 06:46:22 +08:00
|
|
|
from django import forms
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.forms.formsets import DELETION_FIELD_NAME, BaseFormSet
|
|
|
|
from django.forms.models import (
|
|
|
|
BaseModelFormSet,
|
|
|
|
inlineformset_factory,
|
|
|
|
modelform_factory,
|
|
|
|
modelformset_factory,
|
|
|
|
)
|
2013-09-17 00:52:05 +08:00
|
|
|
from django.forms.utils import ErrorDict, ErrorList
|
2009-05-13 22:04:29 +08:00
|
|
|
from django.test import TestCase
|
|
|
|
|
2014-07-09 03:58:14 +08:00
|
|
|
from .models import (
|
2018-12-12 03:41:21 +08:00
|
|
|
Host,
|
|
|
|
Manager,
|
|
|
|
Network,
|
|
|
|
ProfileNetwork,
|
|
|
|
Restaurant,
|
|
|
|
User,
|
|
|
|
UserPreferences,
|
|
|
|
UserProfile,
|
|
|
|
UserSite,
|
2014-07-09 03:58:14 +08:00
|
|
|
)
|
2009-05-13 22:04:29 +08:00
|
|
|
|
2010-09-11 06:46:22 +08:00
|
|
|
|
2009-05-13 22:04:29 +08:00
|
|
|
class InlineFormsetTests(TestCase):
|
|
|
|
def test_formset_over_to_field(self):
|
|
|
|
"""
|
|
|
|
A formset over a ForeignKey with a to_field can be saved.
|
2022-02-04 15:08:27 +08:00
|
|
|
"""
|
2013-02-22 05:56:55 +08:00
|
|
|
Form = modelform_factory(User, fields="__all__")
|
|
|
|
FormSet = inlineformset_factory(User, UserSite, fields="__all__")
|
2009-05-15 20:44:51 +08:00
|
|
|
|
|
|
|
# Instantiate the Form and FormSet to prove
|
|
|
|
# you can create a form with no data
|
|
|
|
form = Form()
|
|
|
|
form_set = FormSet(instance=User())
|
|
|
|
|
|
|
|
# Now create a new User and UserSite instance
|
2009-05-13 22:04:29 +08:00
|
|
|
data = {
|
2012-06-08 00:08:47 +08:00
|
|
|
"serial": "1",
|
|
|
|
"username": "apollo13",
|
|
|
|
"usersite_set-TOTAL_FORMS": "1",
|
|
|
|
"usersite_set-INITIAL_FORMS": "0",
|
|
|
|
"usersite_set-MAX_NUM_FORMS": "0",
|
|
|
|
"usersite_set-0-data": "10",
|
|
|
|
"usersite_set-0-user": "apollo13",
|
2009-05-13 22:04:29 +08:00
|
|
|
}
|
2009-05-15 20:44:51 +08:00
|
|
|
user = User()
|
|
|
|
form = Form(data)
|
|
|
|
if form.is_valid():
|
|
|
|
user = form.save()
|
|
|
|
else:
|
|
|
|
self.fail("Errors found on form:%s" % form_set)
|
|
|
|
|
2009-05-13 22:04:29 +08:00
|
|
|
form_set = FormSet(data, instance=user)
|
|
|
|
if form_set.is_valid():
|
|
|
|
form_set.save()
|
2022-02-22 17:29:38 +08:00
|
|
|
usersite = UserSite.objects.values()
|
2009-05-15 20:44:51 +08:00
|
|
|
self.assertEqual(usersite[0]["data"], 10)
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(usersite[0]["user_id"], "apollo13")
|
2009-05-15 20:44:51 +08:00
|
|
|
else:
|
|
|
|
self.fail("Errors found on formset:%s" % form_set.errors)
|
|
|
|
|
|
|
|
# Now update the UserSite instance
|
|
|
|
data = {
|
2012-06-08 00:08:47 +08:00
|
|
|
"usersite_set-TOTAL_FORMS": "1",
|
|
|
|
"usersite_set-INITIAL_FORMS": "1",
|
|
|
|
"usersite_set-MAX_NUM_FORMS": "0",
|
2016-12-29 23:27:49 +08:00
|
|
|
"usersite_set-0-id": str(usersite[0]["id"]),
|
2012-06-08 00:08:47 +08:00
|
|
|
"usersite_set-0-data": "11",
|
|
|
|
"usersite_set-0-user": "apollo13",
|
2009-05-15 20:44:51 +08:00
|
|
|
}
|
|
|
|
form_set = FormSet(data, instance=user)
|
|
|
|
if form_set.is_valid():
|
|
|
|
form_set.save()
|
2022-02-22 17:29:38 +08:00
|
|
|
usersite = UserSite.objects.values()
|
2009-05-15 20:44:51 +08:00
|
|
|
self.assertEqual(usersite[0]["data"], 11)
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(usersite[0]["user_id"], "apollo13")
|
2009-05-15 20:44:51 +08:00
|
|
|
else:
|
|
|
|
self.fail("Errors found on formset:%s" % form_set.errors)
|
|
|
|
|
|
|
|
# Now add a new UserSite instance
|
|
|
|
data = {
|
2012-06-08 00:08:47 +08:00
|
|
|
"usersite_set-TOTAL_FORMS": "2",
|
|
|
|
"usersite_set-INITIAL_FORMS": "1",
|
|
|
|
"usersite_set-MAX_NUM_FORMS": "0",
|
2016-12-29 23:27:49 +08:00
|
|
|
"usersite_set-0-id": str(usersite[0]["id"]),
|
2012-06-08 00:08:47 +08:00
|
|
|
"usersite_set-0-data": "11",
|
|
|
|
"usersite_set-0-user": "apollo13",
|
|
|
|
"usersite_set-1-data": "42",
|
|
|
|
"usersite_set-1-user": "apollo13",
|
2009-05-15 20:44:51 +08:00
|
|
|
}
|
|
|
|
form_set = FormSet(data, instance=user)
|
|
|
|
if form_set.is_valid():
|
|
|
|
form_set.save()
|
2022-02-22 17:29:38 +08:00
|
|
|
usersite = UserSite.objects.values().order_by("data")
|
2009-05-15 20:44:51 +08:00
|
|
|
self.assertEqual(usersite[0]["data"], 11)
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(usersite[0]["user_id"], "apollo13")
|
2009-05-15 20:44:51 +08:00
|
|
|
self.assertEqual(usersite[1]["data"], 42)
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(usersite[1]["user_id"], "apollo13")
|
2009-05-15 20:44:51 +08:00
|
|
|
else:
|
|
|
|
self.fail("Errors found on formset:%s" % form_set.errors)
|
|
|
|
|
|
|
|
def test_formset_over_inherited_model(self):
|
|
|
|
"""
|
|
|
|
A formset over a ForeignKey with a to_field can be saved.
|
2022-02-04 15:08:27 +08:00
|
|
|
"""
|
2013-02-22 05:56:55 +08:00
|
|
|
Form = modelform_factory(Restaurant, fields="__all__")
|
|
|
|
FormSet = inlineformset_factory(Restaurant, Manager, fields="__all__")
|
2009-05-15 20:44:51 +08:00
|
|
|
|
|
|
|
# Instantiate the Form and FormSet to prove
|
|
|
|
# you can create a form with no data
|
|
|
|
form = Form()
|
|
|
|
form_set = FormSet(instance=Restaurant())
|
|
|
|
|
|
|
|
# Now create a new Restaurant and Manager instance
|
|
|
|
data = {
|
2012-06-08 00:08:47 +08:00
|
|
|
"name": "Guido's House of Pasta",
|
|
|
|
"manager_set-TOTAL_FORMS": "1",
|
|
|
|
"manager_set-INITIAL_FORMS": "0",
|
|
|
|
"manager_set-MAX_NUM_FORMS": "0",
|
|
|
|
"manager_set-0-name": "Guido Van Rossum",
|
2009-05-15 20:44:51 +08:00
|
|
|
}
|
|
|
|
restaurant = User()
|
|
|
|
form = Form(data)
|
|
|
|
if form.is_valid():
|
|
|
|
restaurant = form.save()
|
|
|
|
else:
|
|
|
|
self.fail("Errors found on form:%s" % form_set)
|
|
|
|
|
|
|
|
form_set = FormSet(data, instance=restaurant)
|
|
|
|
if form_set.is_valid():
|
|
|
|
form_set.save()
|
2022-02-22 17:29:38 +08:00
|
|
|
manager = Manager.objects.values()
|
2009-05-15 20:44:51 +08:00
|
|
|
self.assertEqual(manager[0]["name"], "Guido Van Rossum")
|
|
|
|
else:
|
|
|
|
self.fail("Errors found on formset:%s" % form_set.errors)
|
|
|
|
|
|
|
|
# Now update the Manager instance
|
|
|
|
data = {
|
2012-06-08 00:08:47 +08:00
|
|
|
"manager_set-TOTAL_FORMS": "1",
|
|
|
|
"manager_set-INITIAL_FORMS": "1",
|
|
|
|
"manager_set-MAX_NUM_FORMS": "0",
|
2016-12-29 23:27:49 +08:00
|
|
|
"manager_set-0-id": str(manager[0]["id"]),
|
2012-06-08 00:08:47 +08:00
|
|
|
"manager_set-0-name": "Terry Gilliam",
|
2009-05-15 20:44:51 +08:00
|
|
|
}
|
|
|
|
form_set = FormSet(data, instance=restaurant)
|
|
|
|
if form_set.is_valid():
|
|
|
|
form_set.save()
|
2022-02-22 17:29:38 +08:00
|
|
|
manager = Manager.objects.values()
|
2009-05-15 20:44:51 +08:00
|
|
|
self.assertEqual(manager[0]["name"], "Terry Gilliam")
|
|
|
|
else:
|
|
|
|
self.fail("Errors found on formset:%s" % form_set.errors)
|
|
|
|
|
|
|
|
# Now add a new Manager instance
|
|
|
|
data = {
|
2012-06-08 00:08:47 +08:00
|
|
|
"manager_set-TOTAL_FORMS": "2",
|
|
|
|
"manager_set-INITIAL_FORMS": "1",
|
|
|
|
"manager_set-MAX_NUM_FORMS": "0",
|
2016-12-29 23:27:49 +08:00
|
|
|
"manager_set-0-id": str(manager[0]["id"]),
|
2012-06-08 00:08:47 +08:00
|
|
|
"manager_set-0-name": "Terry Gilliam",
|
|
|
|
"manager_set-1-name": "John Cleese",
|
2009-05-15 20:44:51 +08:00
|
|
|
}
|
|
|
|
form_set = FormSet(data, instance=restaurant)
|
|
|
|
if form_set.is_valid():
|
|
|
|
form_set.save()
|
2022-02-22 17:29:38 +08:00
|
|
|
manager = Manager.objects.values().order_by("name")
|
2009-05-15 20:44:51 +08:00
|
|
|
self.assertEqual(manager[0]["name"], "John Cleese")
|
|
|
|
self.assertEqual(manager[1]["name"], "Terry Gilliam")
|
2009-05-13 22:04:29 +08:00
|
|
|
else:
|
2009-05-15 20:44:51 +08:00
|
|
|
self.fail("Errors found on formset:%s" % form_set.errors)
|
2009-10-30 17:11:56 +08:00
|
|
|
|
2014-07-09 03:58:14 +08:00
|
|
|
def test_inline_model_with_to_field(self):
|
|
|
|
"""
|
|
|
|
#13794 --- An inline model with a to_field of a formset with instance
|
|
|
|
has working relations.
|
|
|
|
"""
|
|
|
|
FormSet = inlineformset_factory(User, UserSite, exclude=("is_superuser",))
|
|
|
|
|
|
|
|
user = User.objects.create(username="guido", serial=1337)
|
|
|
|
UserSite.objects.create(user=user, data=10)
|
|
|
|
formset = FormSet(instance=user)
|
|
|
|
|
|
|
|
# Testing the inline model's relation
|
|
|
|
self.assertEqual(formset[0].instance.user_id, "guido")
|
|
|
|
|
2018-12-12 03:41:21 +08:00
|
|
|
def test_inline_model_with_primary_to_field(self):
|
|
|
|
"""An inline model with a OneToOneField with to_field & primary key."""
|
|
|
|
FormSet = inlineformset_factory(
|
|
|
|
User, UserPreferences, exclude=("is_superuser",)
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2018-12-12 03:41:21 +08:00
|
|
|
user = User.objects.create(username="guido", serial=1337)
|
|
|
|
UserPreferences.objects.create(user=user, favorite_number=10)
|
|
|
|
formset = FormSet(instance=user)
|
|
|
|
self.assertEqual(formset[0].fields["user"].initial, "guido")
|
|
|
|
|
2014-07-09 03:58:14 +08:00
|
|
|
def test_inline_model_with_to_field_to_rel(self):
|
|
|
|
"""
|
|
|
|
#13794 --- An inline model with a to_field to a related field of a
|
|
|
|
formset with instance has working relations.
|
|
|
|
"""
|
|
|
|
FormSet = inlineformset_factory(UserProfile, ProfileNetwork, exclude=[])
|
|
|
|
|
|
|
|
user = User.objects.create(username="guido", serial=1337, pk=1)
|
|
|
|
self.assertEqual(user.pk, 1)
|
|
|
|
profile = UserProfile.objects.create(user=user, about="about", pk=2)
|
|
|
|
self.assertEqual(profile.pk, 2)
|
|
|
|
ProfileNetwork.objects.create(profile=profile, network=10, identifier=10)
|
|
|
|
formset = FormSet(instance=profile)
|
|
|
|
|
|
|
|
# Testing the inline model's relation
|
|
|
|
self.assertEqual(formset[0].instance.profile_id, 1)
|
|
|
|
|
2009-10-30 17:11:56 +08:00
|
|
|
def test_formset_with_none_instance(self):
|
|
|
|
"A formset with instance=None can be created. Regression for #11872"
|
2013-02-22 05:56:55 +08:00
|
|
|
Form = modelform_factory(User, fields="__all__")
|
|
|
|
FormSet = inlineformset_factory(User, UserSite, fields="__all__")
|
2009-10-30 17:11:56 +08:00
|
|
|
|
|
|
|
# Instantiate the Form and FormSet to prove
|
|
|
|
# you can create a formset with an instance of None
|
2013-10-19 20:31:38 +08:00
|
|
|
Form(instance=None)
|
|
|
|
FormSet(instance=None)
|
2010-09-11 06:46:22 +08:00
|
|
|
|
2010-10-13 12:46:33 +08:00
|
|
|
def test_empty_fields_on_modelformset(self):
|
2015-09-12 07:33:12 +08:00
|
|
|
"""
|
|
|
|
No fields passed to modelformset_factory() should result in no fields
|
|
|
|
on returned forms except for the id (#14119).
|
|
|
|
"""
|
2010-10-13 12:46:33 +08:00
|
|
|
UserFormSet = modelformset_factory(User, fields=())
|
|
|
|
formset = UserFormSet()
|
|
|
|
for form in formset.forms:
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn("id", form.fields)
|
2010-10-13 12:46:33 +08:00
|
|
|
self.assertEqual(len(form.fields), 1)
|
|
|
|
|
2011-01-25 12:52:54 +08:00
|
|
|
def test_save_as_new_with_new_inlines(self):
|
|
|
|
"""
|
|
|
|
Existing and new inlines are saved with save_as_new.
|
|
|
|
|
|
|
|
Regression for #14938.
|
|
|
|
"""
|
|
|
|
efnet = Network.objects.create(name="EFNet")
|
|
|
|
host1 = Host.objects.create(hostname="irc.he.net", network=efnet)
|
|
|
|
|
2013-02-22 05:56:55 +08:00
|
|
|
HostFormSet = inlineformset_factory(Network, Host, fields="__all__")
|
2011-01-25 12:52:54 +08:00
|
|
|
|
|
|
|
# Add a new host, modify previous host, and save-as-new
|
|
|
|
data = {
|
2012-06-08 00:08:47 +08:00
|
|
|
"host_set-TOTAL_FORMS": "2",
|
|
|
|
"host_set-INITIAL_FORMS": "1",
|
|
|
|
"host_set-MAX_NUM_FORMS": "0",
|
2016-12-29 23:27:49 +08:00
|
|
|
"host_set-0-id": str(host1.id),
|
2012-06-08 00:08:47 +08:00
|
|
|
"host_set-0-hostname": "tranquility.hub.dal.net",
|
|
|
|
"host_set-1-hostname": "matrix.de.eu.dal.net",
|
2011-01-25 12:52:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# To save a formset as new, it needs a new hub instance
|
|
|
|
dalnet = Network.objects.create(name="DALnet")
|
|
|
|
formset = HostFormSet(data, instance=dalnet, save_as_new=True)
|
|
|
|
|
|
|
|
self.assertTrue(formset.is_valid())
|
|
|
|
formset.save()
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(
|
2011-01-25 12:52:54 +08:00
|
|
|
dalnet.host_set.order_by("hostname"),
|
2020-10-19 00:29:52 +08:00
|
|
|
Host.objects.filter(
|
|
|
|
hostname__in=[
|
|
|
|
"matrix.de.eu.dal.net",
|
|
|
|
"tranquility.hub.dal.net",
|
|
|
|
]
|
|
|
|
).order_by("hostname"),
|
2013-10-18 17:02:43 +08:00
|
|
|
)
|
2011-01-25 12:52:54 +08:00
|
|
|
|
2012-01-15 09:36:14 +08:00
|
|
|
def test_initial_data(self):
|
|
|
|
user = User.objects.create(username="bibi", serial=1)
|
|
|
|
UserSite.objects.create(user=user, data=7)
|
2013-02-22 05:56:55 +08:00
|
|
|
FormSet = inlineformset_factory(User, UserSite, extra=2, fields="__all__")
|
2012-01-15 09:36:14 +08:00
|
|
|
|
|
|
|
formset = FormSet(instance=user, initial=[{"data": 41}, {"data": 42}])
|
|
|
|
self.assertEqual(formset.forms[0].initial["data"], 7)
|
|
|
|
self.assertEqual(formset.extra_forms[0].initial["data"], 41)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn('value="42"', formset.extra_forms[1].as_p())
|
2012-01-15 09:36:14 +08:00
|
|
|
|
|
|
|
|
2011-02-05 14:33:16 +08:00
|
|
|
class FormsetTests(TestCase):
|
|
|
|
def test_error_class(self):
|
2022-02-04 03:24:19 +08:00
|
|
|
"""
|
2011-02-05 14:33:16 +08:00
|
|
|
Test the type of Formset and Form error attributes
|
2022-02-04 03:24:19 +08:00
|
|
|
"""
|
2013-02-22 05:56:55 +08:00
|
|
|
Formset = modelformset_factory(User, fields="__all__")
|
2011-02-05 14:33:16 +08:00
|
|
|
data = {
|
2012-06-08 00:08:47 +08:00
|
|
|
"form-TOTAL_FORMS": "2",
|
|
|
|
"form-INITIAL_FORMS": "0",
|
|
|
|
"form-MAX_NUM_FORMS": "0",
|
2011-02-05 14:33:16 +08:00
|
|
|
"form-0-id": "",
|
2012-06-08 00:08:47 +08:00
|
|
|
"form-0-username": "apollo13",
|
|
|
|
"form-0-serial": "1",
|
2011-02-05 14:33:16 +08:00
|
|
|
"form-1-id": "",
|
2012-06-08 00:08:47 +08:00
|
|
|
"form-1-username": "apollo13",
|
|
|
|
"form-1-serial": "2",
|
2011-02-05 14:33:16 +08:00
|
|
|
}
|
|
|
|
formset = Formset(data)
|
|
|
|
# check if the returned error classes are correct
|
|
|
|
# note: formset.errors returns a list as documented
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(formset.errors, list)
|
|
|
|
self.assertIsInstance(formset.non_form_errors(), ErrorList)
|
2011-02-05 14:33:16 +08:00
|
|
|
for form in formset.forms:
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(form.errors, ErrorDict)
|
|
|
|
self.assertIsInstance(form.non_field_errors(), ErrorList)
|
2010-09-11 06:46:22 +08:00
|
|
|
|
2012-01-15 09:36:14 +08:00
|
|
|
def test_initial_data(self):
|
|
|
|
User.objects.create(username="bibi", serial=1)
|
2013-02-22 05:56:55 +08:00
|
|
|
Formset = modelformset_factory(User, fields="__all__", extra=2)
|
2012-06-08 00:08:47 +08:00
|
|
|
formset = Formset(initial=[{"username": "apollo11"}, {"username": "apollo12"}])
|
2012-01-15 09:36:14 +08:00
|
|
|
self.assertEqual(formset.forms[0].initial["username"], "bibi")
|
|
|
|
self.assertEqual(formset.extra_forms[0].initial["username"], "apollo11")
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn('value="apollo12"', formset.extra_forms[1].as_p())
|
2012-01-15 09:36:14 +08:00
|
|
|
|
2012-02-05 00:05:39 +08:00
|
|
|
def test_extraneous_query_is_not_run(self):
|
2013-02-22 05:56:55 +08:00
|
|
|
Formset = modelformset_factory(Network, fields="__all__")
|
2019-01-03 07:18:19 +08:00
|
|
|
data = {
|
|
|
|
"test-TOTAL_FORMS": "1",
|
|
|
|
"test-INITIAL_FORMS": "0",
|
|
|
|
"test-MAX_NUM_FORMS": "",
|
|
|
|
"test-0-name": "Random Place",
|
|
|
|
}
|
2012-02-05 00:05:39 +08:00
|
|
|
with self.assertNumQueries(1):
|
|
|
|
formset = Formset(data, prefix="test")
|
|
|
|
formset.save()
|
|
|
|
|
|
|
|
|
2013-02-05 18:39:35 +08:00
|
|
|
class CustomWidget(forms.widgets.TextInput):
|
2010-09-11 06:46:22 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class UserSiteForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = UserSite
|
2013-02-22 05:56:55 +08:00
|
|
|
fields = "__all__"
|
2013-02-05 18:39:35 +08:00
|
|
|
widgets = {
|
|
|
|
"id": CustomWidget,
|
|
|
|
"data": CustomWidget,
|
|
|
|
}
|
2013-05-18 20:13:00 +08:00
|
|
|
localized_fields = ("data",)
|
2010-09-11 06:46:22 +08:00
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Callback:
|
2010-09-11 06:46:22 +08:00
|
|
|
def __init__(self):
|
|
|
|
self.log = []
|
|
|
|
|
|
|
|
def __call__(self, db_field, **kwargs):
|
|
|
|
self.log.append((db_field, kwargs))
|
|
|
|
return db_field.formfield(**kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class FormfieldCallbackTests(TestCase):
|
|
|
|
"""
|
2013-02-05 18:39:35 +08:00
|
|
|
Regression for #13095 and #17683: Using base forms with widgets
|
|
|
|
defined in Meta should not raise errors and BaseModelForm should respect
|
|
|
|
the specified pk widget.
|
2010-09-11 06:46:22 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
def test_inlineformset_factory_default(self):
|
2013-02-22 05:56:55 +08:00
|
|
|
Formset = inlineformset_factory(
|
|
|
|
User, UserSite, form=UserSiteForm, fields="__all__"
|
|
|
|
)
|
2010-11-22 01:48:52 +08:00
|
|
|
form = Formset().forms[0]
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(form["id"].field.widget, CustomWidget)
|
|
|
|
self.assertIsInstance(form["data"].field.widget, CustomWidget)
|
2013-05-18 20:13:00 +08:00
|
|
|
self.assertFalse(form.fields["id"].localize)
|
|
|
|
self.assertTrue(form.fields["data"].localize)
|
2010-09-11 06:46:22 +08:00
|
|
|
|
|
|
|
def test_modelformset_factory_default(self):
|
|
|
|
Formset = modelformset_factory(UserSite, form=UserSiteForm)
|
2010-11-22 01:48:52 +08:00
|
|
|
form = Formset().forms[0]
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(form["id"].field.widget, CustomWidget)
|
|
|
|
self.assertIsInstance(form["data"].field.widget, CustomWidget)
|
2013-05-18 20:13:00 +08:00
|
|
|
self.assertFalse(form.fields["id"].localize)
|
|
|
|
self.assertTrue(form.fields["data"].localize)
|
2010-09-11 06:46:22 +08:00
|
|
|
|
|
|
|
def assertCallbackCalled(self, callback):
|
|
|
|
id_field, user_field, data_field = UserSite._meta.fields
|
|
|
|
expected_log = [
|
2013-02-05 18:39:35 +08:00
|
|
|
(id_field, {"widget": CustomWidget}),
|
2010-09-11 06:46:22 +08:00
|
|
|
(user_field, {}),
|
2013-05-18 20:13:00 +08:00
|
|
|
(data_field, {"widget": CustomWidget, "localize": True}),
|
2010-09-11 06:46:22 +08:00
|
|
|
]
|
|
|
|
self.assertEqual(callback.log, expected_log)
|
|
|
|
|
|
|
|
def test_inlineformset_custom_callback(self):
|
|
|
|
callback = Callback()
|
|
|
|
inlineformset_factory(
|
|
|
|
User,
|
|
|
|
UserSite,
|
|
|
|
form=UserSiteForm,
|
2013-02-22 05:56:55 +08:00
|
|
|
formfield_callback=callback,
|
|
|
|
fields="__all__",
|
|
|
|
)
|
2010-09-11 06:46:22 +08:00
|
|
|
self.assertCallbackCalled(callback)
|
|
|
|
|
|
|
|
def test_modelformset_custom_callback(self):
|
|
|
|
callback = Callback()
|
2018-03-16 17:54:34 +08:00
|
|
|
modelformset_factory(UserSite, form=UserSiteForm, formfield_callback=callback)
|
2010-09-11 06:46:22 +08:00
|
|
|
self.assertCallbackCalled(callback)
|
2011-02-21 22:23:02 +08:00
|
|
|
|
2013-11-03 06:50:35 +08:00
|
|
|
|
2011-02-21 22:23:02 +08:00
|
|
|
class BaseCustomDeleteFormSet(BaseFormSet):
|
|
|
|
"""
|
|
|
|
A formset mix-in that lets a form decide if it's to be deleted.
|
|
|
|
Works for BaseFormSets. Also works for ModelFormSets with #14099 fixed.
|
|
|
|
|
|
|
|
form.should_delete() is called. The formset delete field is also suppressed.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2011-02-21 22:23:02 +08:00
|
|
|
def add_fields(self, form, index):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().add_fields(form, index)
|
2011-02-21 22:23:02 +08:00
|
|
|
self.can_delete = True
|
|
|
|
if DELETION_FIELD_NAME in form.fields:
|
|
|
|
del form.fields[DELETION_FIELD_NAME]
|
|
|
|
|
|
|
|
def _should_delete_form(self, form):
|
|
|
|
return hasattr(form, "should_delete") and form.should_delete()
|
|
|
|
|
|
|
|
|
|
|
|
class FormfieldShouldDeleteFormTests(TestCase):
|
|
|
|
"""
|
|
|
|
BaseModelFormSet should use ModelFormSet method _should_delete_form.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class BaseCustomDeleteModelFormSet(BaseModelFormSet, BaseCustomDeleteFormSet):
|
|
|
|
"""Model FormSet with CustomDelete MixIn"""
|
|
|
|
|
|
|
|
class CustomDeleteUserForm(forms.ModelForm):
|
|
|
|
"""A model form with a 'should_delete' method"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2011-02-21 22:23:02 +08:00
|
|
|
class Meta:
|
|
|
|
model = User
|
2013-02-22 05:56:55 +08:00
|
|
|
fields = "__all__"
|
2011-02-21 22:23:02 +08:00
|
|
|
|
|
|
|
def should_delete(self):
|
2020-11-02 22:48:53 +08:00
|
|
|
"""Delete form if odd serial."""
|
|
|
|
return self.instance.serial % 2 != 0
|
2011-02-21 22:23:02 +08:00
|
|
|
|
|
|
|
NormalFormset = modelformset_factory(
|
|
|
|
User, form=CustomDeleteUserForm, can_delete=True
|
|
|
|
)
|
|
|
|
DeleteFormset = modelformset_factory(
|
|
|
|
User, form=CustomDeleteUserForm, formset=BaseCustomDeleteModelFormSet
|
|
|
|
)
|
|
|
|
|
|
|
|
data = {
|
2013-10-20 07:33:10 +08:00
|
|
|
"form-TOTAL_FORMS": "4",
|
|
|
|
"form-INITIAL_FORMS": "0",
|
|
|
|
"form-MAX_NUM_FORMS": "4",
|
|
|
|
"form-0-username": "John",
|
|
|
|
"form-0-serial": "1",
|
|
|
|
"form-1-username": "Paul",
|
|
|
|
"form-1-serial": "2",
|
|
|
|
"form-2-username": "George",
|
|
|
|
"form-2-serial": "3",
|
|
|
|
"form-3-username": "Ringo",
|
|
|
|
"form-3-serial": "5",
|
|
|
|
}
|
2011-02-21 22:23:02 +08:00
|
|
|
|
|
|
|
delete_all_ids = {
|
2013-10-20 07:33:10 +08:00
|
|
|
"form-0-DELETE": "1",
|
|
|
|
"form-1-DELETE": "1",
|
|
|
|
"form-2-DELETE": "1",
|
|
|
|
"form-3-DELETE": "1",
|
|
|
|
}
|
2011-02-21 22:23:02 +08:00
|
|
|
|
|
|
|
def test_init_database(self):
|
|
|
|
"""Add test data to database via formset"""
|
|
|
|
formset = self.NormalFormset(self.data)
|
|
|
|
self.assertTrue(formset.is_valid())
|
|
|
|
self.assertEqual(len(formset.save()), 4)
|
|
|
|
|
|
|
|
def test_no_delete(self):
|
|
|
|
"""Verify base formset doesn't modify database"""
|
|
|
|
# reload database
|
|
|
|
self.test_init_database()
|
|
|
|
|
|
|
|
# pass standard data dict & see none updated
|
|
|
|
data = dict(self.data)
|
2011-03-04 08:23:00 +08:00
|
|
|
data["form-INITIAL_FORMS"] = 4
|
2014-12-07 05:00:09 +08:00
|
|
|
data.update(
|
2022-04-14 18:12:13 +08:00
|
|
|
{
|
|
|
|
"form-%d-id" % i: user.pk
|
|
|
|
for i, user in enumerate(User.objects.order_by("pk"))
|
|
|
|
}
|
2014-12-07 05:00:09 +08:00
|
|
|
)
|
2011-02-21 22:23:02 +08:00
|
|
|
formset = self.NormalFormset(data, queryset=User.objects.all())
|
|
|
|
self.assertTrue(formset.is_valid())
|
|
|
|
self.assertEqual(len(formset.save()), 0)
|
|
|
|
self.assertEqual(len(User.objects.all()), 4)
|
|
|
|
|
|
|
|
def test_all_delete(self):
|
|
|
|
"""Verify base formset honors DELETE field"""
|
|
|
|
# reload database
|
|
|
|
self.test_init_database()
|
|
|
|
|
|
|
|
# create data dict with all fields marked for deletion
|
|
|
|
data = dict(self.data)
|
2011-03-04 08:23:00 +08:00
|
|
|
data["form-INITIAL_FORMS"] = 4
|
2014-12-07 05:00:09 +08:00
|
|
|
data.update(
|
2013-10-27 03:15:03 +08:00
|
|
|
{"form-%d-id" % i: user.pk for i, user in enumerate(User.objects.all())}
|
2014-12-07 05:00:09 +08:00
|
|
|
)
|
2011-02-21 22:23:02 +08:00
|
|
|
data.update(self.delete_all_ids)
|
|
|
|
formset = self.NormalFormset(data, queryset=User.objects.all())
|
|
|
|
self.assertTrue(formset.is_valid())
|
|
|
|
self.assertEqual(len(formset.save()), 0)
|
|
|
|
self.assertEqual(len(User.objects.all()), 0)
|
|
|
|
|
|
|
|
def test_custom_delete(self):
|
|
|
|
"""Verify DeleteFormset ignores DELETE field and uses form method"""
|
|
|
|
# reload database
|
|
|
|
self.test_init_database()
|
|
|
|
|
|
|
|
# Create formset with custom Delete function
|
|
|
|
# create data dict with all fields marked for deletion
|
|
|
|
data = dict(self.data)
|
2011-03-04 08:23:00 +08:00
|
|
|
data["form-INITIAL_FORMS"] = 4
|
2014-12-07 05:00:09 +08:00
|
|
|
data.update(
|
2022-04-14 18:12:13 +08:00
|
|
|
{
|
|
|
|
"form-%d-id" % i: user.pk
|
|
|
|
for i, user in enumerate(User.objects.order_by("pk"))
|
|
|
|
}
|
2014-12-07 05:00:09 +08:00
|
|
|
)
|
2011-02-21 22:23:02 +08:00
|
|
|
data.update(self.delete_all_ids)
|
|
|
|
formset = self.DeleteFormset(data, queryset=User.objects.all())
|
|
|
|
|
2020-11-02 22:48:53 +08:00
|
|
|
# Three with odd serial values were deleted.
|
2011-02-21 22:23:02 +08:00
|
|
|
self.assertTrue(formset.is_valid())
|
|
|
|
self.assertEqual(len(formset.save()), 0)
|
2020-11-02 22:48:53 +08:00
|
|
|
self.assertEqual(User.objects.count(), 1)
|
2011-02-21 22:23:02 +08:00
|
|
|
|
2020-11-02 22:48:53 +08:00
|
|
|
# No odd serial values left.
|
|
|
|
odd_serials = [user.serial for user in User.objects.all() if user.serial % 2]
|
|
|
|
self.assertEqual(len(odd_serials), 0)
|
2013-10-31 03:14:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
class RedeleteTests(TestCase):
|
|
|
|
def test_resubmit(self):
|
|
|
|
u = User.objects.create(username="foo", serial=1)
|
|
|
|
us = UserSite.objects.create(user=u, data=7)
|
|
|
|
formset_cls = inlineformset_factory(User, UserSite, fields="__all__")
|
|
|
|
data = {
|
|
|
|
"serial": "1",
|
|
|
|
"username": "foo",
|
|
|
|
"usersite_set-TOTAL_FORMS": "1",
|
|
|
|
"usersite_set-INITIAL_FORMS": "1",
|
|
|
|
"usersite_set-MAX_NUM_FORMS": "1",
|
2016-12-29 23:27:49 +08:00
|
|
|
"usersite_set-0-id": str(us.pk),
|
2013-10-31 03:14:23 +08:00
|
|
|
"usersite_set-0-data": "7",
|
|
|
|
"usersite_set-0-user": "foo",
|
|
|
|
"usersite_set-0-DELETE": "1",
|
|
|
|
}
|
|
|
|
formset = formset_cls(data, instance=u)
|
|
|
|
self.assertTrue(formset.is_valid())
|
|
|
|
formset.save()
|
|
|
|
self.assertEqual(UserSite.objects.count(), 0)
|
|
|
|
formset = formset_cls(data, instance=u)
|
|
|
|
# Even if the "us" object isn't in the DB any more, the form
|
|
|
|
# validates.
|
|
|
|
self.assertTrue(formset.is_valid())
|
|
|
|
formset.save()
|
|
|
|
self.assertEqual(UserSite.objects.count(), 0)
|
|
|
|
|
|
|
|
def test_delete_already_deleted(self):
|
|
|
|
u = User.objects.create(username="foo", serial=1)
|
|
|
|
us = UserSite.objects.create(user=u, data=7)
|
|
|
|
formset_cls = inlineformset_factory(User, UserSite, fields="__all__")
|
|
|
|
data = {
|
|
|
|
"serial": "1",
|
|
|
|
"username": "foo",
|
|
|
|
"usersite_set-TOTAL_FORMS": "1",
|
|
|
|
"usersite_set-INITIAL_FORMS": "1",
|
|
|
|
"usersite_set-MAX_NUM_FORMS": "1",
|
2016-12-29 23:27:49 +08:00
|
|
|
"usersite_set-0-id": str(us.pk),
|
2013-10-31 03:14:23 +08:00
|
|
|
"usersite_set-0-data": "7",
|
|
|
|
"usersite_set-0-user": "foo",
|
|
|
|
"usersite_set-0-DELETE": "1",
|
|
|
|
}
|
|
|
|
formset = formset_cls(data, instance=u)
|
|
|
|
us.delete()
|
|
|
|
self.assertTrue(formset.is_valid())
|
|
|
|
formset.save()
|
|
|
|
self.assertEqual(UserSite.objects.count(), 0)
|