mirror of
https://github.com/django/django.git
synced 2025-08-03 18:38:50 +00:00
Fixed #19733 - deprecated ModelForms without 'fields' or 'exclude', and added '__all__' shortcut
This also updates all dependent functionality, including modelform_factory and modelformset_factory, and the generic views `ModelFormMixin`, `CreateView` and `UpdateView` which gain a new `fields` attribute.
This commit is contained in:
parent
1e37cb37ce
commit
f026a519ae
34 changed files with 578 additions and 201 deletions
|
@ -21,7 +21,7 @@ from .models import (Author, BetterAuthor, Book, BookWithCustomPK,
|
|||
|
||||
class DeletionTests(TestCase):
|
||||
def test_deletion(self):
|
||||
PoetFormSet = modelformset_factory(Poet, can_delete=True)
|
||||
PoetFormSet = modelformset_factory(Poet, fields="__all__", can_delete=True)
|
||||
poet = Poet.objects.create(name='test')
|
||||
data = {
|
||||
'form-TOTAL_FORMS': '1',
|
||||
|
@ -41,7 +41,7 @@ class DeletionTests(TestCase):
|
|||
Make sure that an add form that is filled out, but marked for deletion
|
||||
doesn't cause validation errors.
|
||||
"""
|
||||
PoetFormSet = modelformset_factory(Poet, can_delete=True)
|
||||
PoetFormSet = modelformset_factory(Poet, fields="__all__", can_delete=True)
|
||||
poet = Poet.objects.create(name='test')
|
||||
# One existing untouched and two new unvalid forms
|
||||
data = {
|
||||
|
@ -75,7 +75,7 @@ class DeletionTests(TestCase):
|
|||
Make sure that a change form that is filled out, but marked for deletion
|
||||
doesn't cause validation errors.
|
||||
"""
|
||||
PoetFormSet = modelformset_factory(Poet, can_delete=True)
|
||||
PoetFormSet = modelformset_factory(Poet, fields="__all__", can_delete=True)
|
||||
poet = Poet.objects.create(name='test')
|
||||
data = {
|
||||
'form-TOTAL_FORMS': '1',
|
||||
|
@ -100,7 +100,7 @@ class DeletionTests(TestCase):
|
|||
class ModelFormsetTest(TestCase):
|
||||
def test_simple_save(self):
|
||||
qs = Author.objects.all()
|
||||
AuthorFormSet = modelformset_factory(Author, extra=3)
|
||||
AuthorFormSet = modelformset_factory(Author, fields="__all__", extra=3)
|
||||
|
||||
formset = AuthorFormSet(queryset=qs)
|
||||
self.assertEqual(len(formset.forms), 3)
|
||||
|
@ -138,7 +138,7 @@ class ModelFormsetTest(TestCase):
|
|||
# we'll use it to display them in alphabetical order by name.
|
||||
|
||||
qs = Author.objects.order_by('name')
|
||||
AuthorFormSet = modelformset_factory(Author, extra=1, can_delete=False)
|
||||
AuthorFormSet = modelformset_factory(Author, fields="__all__", extra=1, can_delete=False)
|
||||
|
||||
formset = AuthorFormSet(queryset=qs)
|
||||
self.assertEqual(len(formset.forms), 3)
|
||||
|
@ -176,7 +176,7 @@ class ModelFormsetTest(TestCase):
|
|||
# marked for deletion, make sure we don't save that form.
|
||||
|
||||
qs = Author.objects.order_by('name')
|
||||
AuthorFormSet = modelformset_factory(Author, extra=1, can_delete=True)
|
||||
AuthorFormSet = modelformset_factory(Author, fields="__all__", extra=1, can_delete=True)
|
||||
|
||||
formset = AuthorFormSet(queryset=qs)
|
||||
self.assertEqual(len(formset.forms), 4)
|
||||
|
@ -256,7 +256,7 @@ class ModelFormsetTest(TestCase):
|
|||
|
||||
author4 = Author.objects.create(name='John Steinbeck')
|
||||
|
||||
AuthorMeetingFormSet = modelformset_factory(AuthorMeeting, extra=1, can_delete=True)
|
||||
AuthorMeetingFormSet = modelformset_factory(AuthorMeeting, fields="__all__", extra=1, can_delete=True)
|
||||
data = {
|
||||
'form-TOTAL_FORMS': '2', # the number of forms rendered
|
||||
'form-INITIAL_FORMS': '1', # the number of forms with initial data
|
||||
|
@ -294,22 +294,22 @@ class ModelFormsetTest(TestCase):
|
|||
|
||||
qs = Author.objects.order_by('name')
|
||||
|
||||
AuthorFormSet = modelformset_factory(Author, max_num=None, extra=3)
|
||||
AuthorFormSet = modelformset_factory(Author, fields="__all__", max_num=None, extra=3)
|
||||
formset = AuthorFormSet(queryset=qs)
|
||||
self.assertEqual(len(formset.forms), 6)
|
||||
self.assertEqual(len(formset.extra_forms), 3)
|
||||
|
||||
AuthorFormSet = modelformset_factory(Author, max_num=4, extra=3)
|
||||
AuthorFormSet = modelformset_factory(Author, fields="__all__", max_num=4, extra=3)
|
||||
formset = AuthorFormSet(queryset=qs)
|
||||
self.assertEqual(len(formset.forms), 4)
|
||||
self.assertEqual(len(formset.extra_forms), 1)
|
||||
|
||||
AuthorFormSet = modelformset_factory(Author, max_num=0, extra=3)
|
||||
AuthorFormSet = modelformset_factory(Author, fields="__all__", max_num=0, extra=3)
|
||||
formset = AuthorFormSet(queryset=qs)
|
||||
self.assertEqual(len(formset.forms), 3)
|
||||
self.assertEqual(len(formset.extra_forms), 0)
|
||||
|
||||
AuthorFormSet = modelformset_factory(Author, max_num=None)
|
||||
AuthorFormSet = modelformset_factory(Author, fields="__all__", max_num=None)
|
||||
formset = AuthorFormSet(queryset=qs)
|
||||
self.assertQuerysetEqual(formset.get_queryset(), [
|
||||
'<Author: Charles Baudelaire>',
|
||||
|
@ -317,7 +317,7 @@ class ModelFormsetTest(TestCase):
|
|||
'<Author: Walt Whitman>',
|
||||
])
|
||||
|
||||
AuthorFormSet = modelformset_factory(Author, max_num=0)
|
||||
AuthorFormSet = modelformset_factory(Author, fields="__all__", max_num=0)
|
||||
formset = AuthorFormSet(queryset=qs)
|
||||
self.assertQuerysetEqual(formset.get_queryset(), [
|
||||
'<Author: Charles Baudelaire>',
|
||||
|
@ -325,7 +325,7 @@ class ModelFormsetTest(TestCase):
|
|||
'<Author: Walt Whitman>',
|
||||
])
|
||||
|
||||
AuthorFormSet = modelformset_factory(Author, max_num=4)
|
||||
AuthorFormSet = modelformset_factory(Author, fields="__all__", max_num=4)
|
||||
formset = AuthorFormSet(queryset=qs)
|
||||
self.assertQuerysetEqual(formset.get_queryset(), [
|
||||
'<Author: Charles Baudelaire>',
|
||||
|
@ -343,7 +343,7 @@ class ModelFormsetTest(TestCase):
|
|||
author.save()
|
||||
return author
|
||||
|
||||
PoetFormSet = modelformset_factory(Poet, form=PoetForm)
|
||||
PoetFormSet = modelformset_factory(Poet, fields="__all__", form=PoetForm)
|
||||
|
||||
data = {
|
||||
'form-TOTAL_FORMS': '3', # the number of forms rendered
|
||||
|
@ -387,7 +387,7 @@ class ModelFormsetTest(TestCase):
|
|||
self.assertFalse("subtitle" in formset.forms[0].fields)
|
||||
|
||||
def test_model_inheritance(self):
|
||||
BetterAuthorFormSet = modelformset_factory(BetterAuthor)
|
||||
BetterAuthorFormSet = modelformset_factory(BetterAuthor, fields="__all__")
|
||||
formset = BetterAuthorFormSet()
|
||||
self.assertEqual(len(formset.forms), 1)
|
||||
self.assertHTMLEqual(formset.forms[0].as_p(),
|
||||
|
@ -440,7 +440,7 @@ class ModelFormsetTest(TestCase):
|
|||
# We can also create a formset that is tied to a parent model. This is
|
||||
# how the admin system's edit inline functionality works.
|
||||
|
||||
AuthorBooksFormSet = inlineformset_factory(Author, Book, can_delete=False, extra=3)
|
||||
AuthorBooksFormSet = inlineformset_factory(Author, Book, can_delete=False, extra=3, fields="__all__")
|
||||
author = Author.objects.create(name='Charles Baudelaire')
|
||||
|
||||
formset = AuthorBooksFormSet(instance=author)
|
||||
|
@ -474,7 +474,7 @@ class ModelFormsetTest(TestCase):
|
|||
# another one. This time though, an edit form will be available for
|
||||
# every existing book.
|
||||
|
||||
AuthorBooksFormSet = inlineformset_factory(Author, Book, can_delete=False, extra=2)
|
||||
AuthorBooksFormSet = inlineformset_factory(Author, Book, can_delete=False, extra=2, fields="__all__")
|
||||
author = Author.objects.get(name='Charles Baudelaire')
|
||||
|
||||
formset = AuthorBooksFormSet(instance=author)
|
||||
|
@ -514,7 +514,7 @@ class ModelFormsetTest(TestCase):
|
|||
def test_inline_formsets_save_as_new(self):
|
||||
# The save_as_new parameter lets you re-associate the data to a new
|
||||
# instance. This is used in the admin for save_as functionality.
|
||||
AuthorBooksFormSet = inlineformset_factory(Author, Book, can_delete=False, extra=2)
|
||||
AuthorBooksFormSet = inlineformset_factory(Author, Book, can_delete=False, extra=2, fields="__all__")
|
||||
author = Author.objects.create(name='Charles Baudelaire')
|
||||
|
||||
data = {
|
||||
|
@ -553,7 +553,7 @@ class ModelFormsetTest(TestCase):
|
|||
# primary key that is not the fk to the parent object.
|
||||
self.maxDiff = 1024
|
||||
|
||||
AuthorBooksFormSet2 = inlineformset_factory(Author, BookWithCustomPK, can_delete=False, extra=1)
|
||||
AuthorBooksFormSet2 = inlineformset_factory(Author, BookWithCustomPK, can_delete=False, extra=1, fields="__all__")
|
||||
author = Author.objects.create(pk=1, name='Charles Baudelaire')
|
||||
|
||||
formset = AuthorBooksFormSet2(instance=author)
|
||||
|
@ -585,7 +585,7 @@ class ModelFormsetTest(TestCase):
|
|||
# Test inline formsets where the inline-edited object uses multi-table
|
||||
# inheritance, thus has a non AutoField yet auto-created primary key.
|
||||
|
||||
AuthorBooksFormSet3 = inlineformset_factory(Author, AlternateBook, can_delete=False, extra=1)
|
||||
AuthorBooksFormSet3 = inlineformset_factory(Author, AlternateBook, can_delete=False, extra=1, fields="__all__")
|
||||
author = Author.objects.create(pk=1, name='Charles Baudelaire')
|
||||
|
||||
formset = AuthorBooksFormSet3(instance=author)
|
||||
|
@ -616,7 +616,7 @@ class ModelFormsetTest(TestCase):
|
|||
# Test inline formsets where the inline-edited object has a
|
||||
# unique_together constraint with a nullable member
|
||||
|
||||
AuthorBooksFormSet4 = inlineformset_factory(Author, BookWithOptionalAltEditor, can_delete=False, extra=2)
|
||||
AuthorBooksFormSet4 = inlineformset_factory(Author, BookWithOptionalAltEditor, can_delete=False, extra=2, fields="__all__")
|
||||
author = Author.objects.create(pk=1, name='Charles Baudelaire')
|
||||
|
||||
data = {
|
||||
|
@ -640,7 +640,7 @@ class ModelFormsetTest(TestCase):
|
|||
self.assertEqual(book2.title, 'Les Fleurs du Mal')
|
||||
|
||||
def test_inline_formsets_with_custom_save_method(self):
|
||||
AuthorBooksFormSet = inlineformset_factory(Author, Book, can_delete=False, extra=2)
|
||||
AuthorBooksFormSet = inlineformset_factory(Author, Book, can_delete=False, extra=2, fields="__all__")
|
||||
author = Author.objects.create(pk=1, name='Charles Baudelaire')
|
||||
book1 = Book.objects.create(pk=1, author=author, title='Les Paradis Artificiels')
|
||||
book2 = Book.objects.create(pk=2, author=author, title='Les Fleurs du Mal')
|
||||
|
@ -655,7 +655,7 @@ class ModelFormsetTest(TestCase):
|
|||
poem.save()
|
||||
return poem
|
||||
|
||||
PoemFormSet = inlineformset_factory(Poet, Poem, form=PoemForm)
|
||||
PoemFormSet = inlineformset_factory(Poet, Poem, form=PoemForm, fields="__all__")
|
||||
|
||||
data = {
|
||||
'poem_set-TOTAL_FORMS': '3', # the number of forms rendered
|
||||
|
@ -732,7 +732,7 @@ class ModelFormsetTest(TestCase):
|
|||
def test_custom_pk(self):
|
||||
# We need to ensure that it is displayed
|
||||
|
||||
CustomPrimaryKeyFormSet = modelformset_factory(CustomPrimaryKey)
|
||||
CustomPrimaryKeyFormSet = modelformset_factory(CustomPrimaryKey, fields="__all__")
|
||||
formset = CustomPrimaryKeyFormSet()
|
||||
self.assertEqual(len(formset.forms), 1)
|
||||
self.assertHTMLEqual(formset.forms[0].as_p(),
|
||||
|
@ -743,7 +743,7 @@ class ModelFormsetTest(TestCase):
|
|||
|
||||
place = Place.objects.create(pk=1, name='Giordanos', city='Chicago')
|
||||
|
||||
FormSet = inlineformset_factory(Place, Owner, extra=2, can_delete=False)
|
||||
FormSet = inlineformset_factory(Place, Owner, extra=2, can_delete=False, fields="__all__")
|
||||
formset = FormSet(instance=place)
|
||||
self.assertEqual(len(formset.forms), 2)
|
||||
self.assertHTMLEqual(formset.forms[0].as_p(),
|
||||
|
@ -799,7 +799,7 @@ class ModelFormsetTest(TestCase):
|
|||
|
||||
# Ensure a custom primary key that is a ForeignKey or OneToOneField get rendered for the user to choose.
|
||||
|
||||
FormSet = modelformset_factory(OwnerProfile)
|
||||
FormSet = modelformset_factory(OwnerProfile, fields="__all__")
|
||||
formset = FormSet()
|
||||
self.assertHTMLEqual(formset.forms[0].as_p(),
|
||||
'<p><label for="id_form-0-owner">Owner:</label> <select name="form-0-owner" id="id_form-0-owner">\n'
|
||||
|
@ -811,7 +811,7 @@ class ModelFormsetTest(TestCase):
|
|||
% (owner1.auto_id, owner2.auto_id))
|
||||
|
||||
owner1 = Owner.objects.get(name='Joe Perry')
|
||||
FormSet = inlineformset_factory(Owner, OwnerProfile, max_num=1, can_delete=False)
|
||||
FormSet = inlineformset_factory(Owner, OwnerProfile, max_num=1, can_delete=False, fields="__all__")
|
||||
self.assertEqual(FormSet.max_num, 1)
|
||||
|
||||
formset = FormSet(instance=owner1)
|
||||
|
@ -861,7 +861,7 @@ class ModelFormsetTest(TestCase):
|
|||
|
||||
place = Place.objects.create(pk=1, name='Giordanos', city='Chicago')
|
||||
|
||||
FormSet = inlineformset_factory(Place, Location, can_delete=False)
|
||||
FormSet = inlineformset_factory(Place, Location, can_delete=False, fields="__all__")
|
||||
self.assertEqual(FormSet.max_num, 1)
|
||||
|
||||
formset = FormSet(instance=place)
|
||||
|
@ -875,7 +875,7 @@ class ModelFormsetTest(TestCase):
|
|||
self.assertEqual(type(_get_foreign_key(MexicanRestaurant, Owner)), models.ForeignKey)
|
||||
|
||||
def test_unique_validation(self):
|
||||
FormSet = modelformset_factory(Product, extra=1)
|
||||
FormSet = modelformset_factory(Product, fields="__all__", extra=1)
|
||||
data = {
|
||||
'form-TOTAL_FORMS': '1',
|
||||
'form-INITIAL_FORMS': '0',
|
||||
|
@ -915,19 +915,19 @@ class ModelFormsetTest(TestCase):
|
|||
'form-1-quantity': '2',
|
||||
}
|
||||
|
||||
FormSet = modelformset_factory(Price, extra=1, max_num=1, validate_max=True)
|
||||
FormSet = modelformset_factory(Price, fields="__all__", extra=1, max_num=1, validate_max=True)
|
||||
formset = FormSet(data)
|
||||
self.assertFalse(formset.is_valid())
|
||||
self.assertEqual(formset.non_form_errors(), ['Please submit 1 or fewer forms.'])
|
||||
|
||||
# Now test the same thing without the validate_max flag to ensure
|
||||
# default behavior is unchanged
|
||||
FormSet = modelformset_factory(Price, extra=1, max_num=1)
|
||||
FormSet = modelformset_factory(Price, fields="__all__", extra=1, max_num=1)
|
||||
formset = FormSet(data)
|
||||
self.assertTrue(formset.is_valid())
|
||||
|
||||
def test_unique_together_validation(self):
|
||||
FormSet = modelformset_factory(Price, extra=1)
|
||||
FormSet = modelformset_factory(Price, fields="__all__", extra=1)
|
||||
data = {
|
||||
'form-TOTAL_FORMS': '1',
|
||||
'form-INITIAL_FORMS': '0',
|
||||
|
@ -958,7 +958,7 @@ class ModelFormsetTest(TestCase):
|
|||
# Also see bug #8882.
|
||||
|
||||
repository = Repository.objects.create(name='Test Repo')
|
||||
FormSet = inlineformset_factory(Repository, Revision, extra=1)
|
||||
FormSet = inlineformset_factory(Repository, Revision, extra=1, fields="__all__")
|
||||
data = {
|
||||
'revision_set-TOTAL_FORMS': '1',
|
||||
'revision_set-INITIAL_FORMS': '0',
|
||||
|
@ -1007,7 +1007,7 @@ class ModelFormsetTest(TestCase):
|
|||
# Use of callable defaults (see bug #7975).
|
||||
|
||||
person = Person.objects.create(name='Ringo')
|
||||
FormSet = inlineformset_factory(Person, Membership, can_delete=False, extra=1)
|
||||
FormSet = inlineformset_factory(Person, Membership, can_delete=False, extra=1, fields="__all__")
|
||||
formset = FormSet(instance=person)
|
||||
|
||||
# Django will render a hidden field for model fields that have a callable
|
||||
|
@ -1057,11 +1057,12 @@ class ModelFormsetTest(TestCase):
|
|||
date_joined = forms.SplitDateTimeField(initial=now)
|
||||
class Meta:
|
||||
model = Membership
|
||||
fields = "__all__"
|
||||
def __init__(self, **kwargs):
|
||||
super(MembershipForm, self).__init__(**kwargs)
|
||||
self.fields['date_joined'].widget = forms.SplitDateTimeWidget()
|
||||
|
||||
FormSet = inlineformset_factory(Person, Membership, form=MembershipForm, can_delete=False, extra=1)
|
||||
FormSet = inlineformset_factory(Person, Membership, form=MembershipForm, can_delete=False, extra=1, fields="__all__")
|
||||
data = {
|
||||
'membership_set-TOTAL_FORMS': '1',
|
||||
'membership_set-INITIAL_FORMS': '0',
|
||||
|
@ -1081,7 +1082,7 @@ class ModelFormsetTest(TestCase):
|
|||
Player(name="Timmy").save()
|
||||
Player(name="Bobby", team=team).save()
|
||||
|
||||
PlayerInlineFormSet = inlineformset_factory(Team, Player)
|
||||
PlayerInlineFormSet = inlineformset_factory(Team, Player, fields="__all__")
|
||||
formset = PlayerInlineFormSet()
|
||||
self.assertQuerysetEqual(formset.get_queryset(), [])
|
||||
|
||||
|
@ -1101,7 +1102,7 @@ class ModelFormsetTest(TestCase):
|
|||
def test_model_formset_with_initial_model_instance(self):
|
||||
# has_changed should compare model instance and primary key
|
||||
# see #18898
|
||||
FormSet = modelformset_factory(Poem)
|
||||
FormSet = modelformset_factory(Poem, fields='__all__')
|
||||
john_milton = Poet(name="John Milton")
|
||||
john_milton.save()
|
||||
data = {
|
||||
|
@ -1117,7 +1118,7 @@ class ModelFormsetTest(TestCase):
|
|||
def test_model_formset_with_initial_queryset(self):
|
||||
# has_changed should work with queryset and list of pk's
|
||||
# see #18898
|
||||
FormSet = modelformset_factory(AuthorMeeting)
|
||||
FormSet = modelformset_factory(AuthorMeeting, fields='__all__')
|
||||
author = Author.objects.create(pk=1, name='Charles Baudelaire')
|
||||
data = {
|
||||
'form-TOTAL_FORMS': 1,
|
||||
|
@ -1131,7 +1132,7 @@ class ModelFormsetTest(TestCase):
|
|||
self.assertFalse(formset.extra_forms[0].has_changed())
|
||||
|
||||
def test_prevent_duplicates_from_with_the_same_formset(self):
|
||||
FormSet = modelformset_factory(Product, extra=2)
|
||||
FormSet = modelformset_factory(Product, fields="__all__", extra=2)
|
||||
data = {
|
||||
'form-TOTAL_FORMS': 2,
|
||||
'form-INITIAL_FORMS': 0,
|
||||
|
@ -1144,7 +1145,7 @@ class ModelFormsetTest(TestCase):
|
|||
self.assertEqual(formset._non_form_errors,
|
||||
['Please correct the duplicate data for slug.'])
|
||||
|
||||
FormSet = modelformset_factory(Price, extra=2)
|
||||
FormSet = modelformset_factory(Price, fields="__all__", extra=2)
|
||||
data = {
|
||||
'form-TOTAL_FORMS': 2,
|
||||
'form-INITIAL_FORMS': 0,
|
||||
|
@ -1172,7 +1173,7 @@ class ModelFormsetTest(TestCase):
|
|||
formset = FormSet(data)
|
||||
self.assertTrue(formset.is_valid())
|
||||
|
||||
FormSet = inlineformset_factory(Author, Book, extra=0)
|
||||
FormSet = inlineformset_factory(Author, Book, extra=0, fields="__all__")
|
||||
author = Author.objects.create(pk=1, name='Charles Baudelaire')
|
||||
book1 = Book.objects.create(pk=1, author=author, title='Les Paradis Artificiels')
|
||||
book2 = Book.objects.create(pk=2, author=author, title='Les Fleurs du Mal')
|
||||
|
@ -1199,7 +1200,7 @@ class ModelFormsetTest(TestCase):
|
|||
self.assertEqual(formset.errors,
|
||||
[{}, {'__all__': ['Please correct the duplicate values below.']}])
|
||||
|
||||
FormSet = modelformset_factory(Post, extra=2)
|
||||
FormSet = modelformset_factory(Post, fields="__all__", extra=2)
|
||||
data = {
|
||||
'form-TOTAL_FORMS': '2',
|
||||
'form-INITIAL_FORMS': '0',
|
||||
|
@ -1265,7 +1266,7 @@ class TestModelFormsetWidgets(TestCase):
|
|||
widgets = {
|
||||
'name': forms.TextInput(attrs={'class': 'poet'})
|
||||
}
|
||||
PoetFormSet = modelformset_factory(Poet, widgets=widgets)
|
||||
PoetFormSet = modelformset_factory(Poet, fields="__all__", widgets=widgets)
|
||||
form = PoetFormSet.form()
|
||||
self.assertHTMLEqual(
|
||||
"%s" % form['name'],
|
||||
|
@ -1276,7 +1277,7 @@ class TestModelFormsetWidgets(TestCase):
|
|||
widgets = {
|
||||
'title': forms.TextInput(attrs={'class': 'book'})
|
||||
}
|
||||
BookFormSet = inlineformset_factory(Author, Book, widgets=widgets)
|
||||
BookFormSet = inlineformset_factory(Author, Book, widgets=widgets, fields="__all__")
|
||||
form = BookFormSet.form()
|
||||
self.assertHTMLEqual(
|
||||
"%s" % form['title'],
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue