1
0
mirror of https://github.com/django/django.git synced 2025-06-05 11:39:13 +00:00

Avoided creating default form fields in fields_for_model() when declared on form.

This commit is contained in:
Mariusz Felisiak 2023-04-27 15:26:23 +02:00 committed by GitHub
parent 23d24f82a7
commit 720abed343
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 2 deletions

View File

@ -146,6 +146,7 @@ def fields_for_model(
field_classes=None, field_classes=None,
*, *,
apply_limit_choices_to=True, apply_limit_choices_to=True,
form_declared_fields=None,
): ):
""" """
Return a dictionary containing form fields for the given model. Return a dictionary containing form fields for the given model.
@ -176,7 +177,11 @@ def fields_for_model(
``apply_limit_choices_to`` is a boolean indicating if limit_choices_to ``apply_limit_choices_to`` is a boolean indicating if limit_choices_to
should be applied to a field's queryset. should be applied to a field's queryset.
``form_declared_fields`` is a dictionary of form fields created directly on
a form.
""" """
form_declared_fields = form_declared_fields or {}
field_dict = {} field_dict = {}
ignored = [] ignored = []
opts = model._meta opts = model._meta
@ -204,6 +209,9 @@ def fields_for_model(
continue continue
if exclude and f.name in exclude: if exclude and f.name in exclude:
continue continue
if f.name in form_declared_fields:
field_dict[f.name] = form_declared_fields[f.name]
continue
kwargs = {} kwargs = {}
if widgets and f.name in widgets: if widgets and f.name in widgets:
@ -310,6 +318,7 @@ class ModelFormMetaclass(DeclarativeFieldsMetaclass):
opts.field_classes, opts.field_classes,
# limit_choices_to will be applied during ModelForm.__init__(). # limit_choices_to will be applied during ModelForm.__init__().
apply_limit_choices_to=False, apply_limit_choices_to=False,
form_declared_fields=new_class.declared_fields,
) )
# make sure opts.fields doesn't specify an invalid field # make sure opts.fields doesn't specify an invalid field
@ -319,8 +328,7 @@ class ModelFormMetaclass(DeclarativeFieldsMetaclass):
message = "Unknown field(s) (%s) specified for %s" message = "Unknown field(s) (%s) specified for %s"
message %= (", ".join(missing_fields), opts.model.__name__) message %= (", ".join(missing_fields), opts.model.__name__)
raise FieldError(message) raise FieldError(message)
# Override default model fields with any custom declared ones # Include all the other declared fields.
# (plus, include all the other declared fields).
fields.update(new_class.declared_fields) fields.update(new_class.declared_fields)
else: else:
fields = new_class.declared_fields fields = new_class.declared_fields

View File

@ -236,6 +236,15 @@ class ModelFormBaseTest(TestCase):
field_dict = fields_for_model(Person, fields=()) field_dict = fields_for_model(Person, fields=())
self.assertEqual(len(field_dict), 0) self.assertEqual(len(field_dict), 0)
def test_fields_for_model_form_fields(self):
form_declared_fields = CustomWriterForm.declared_fields
field_dict = fields_for_model(
Writer,
fields=["name"],
form_declared_fields=form_declared_fields,
)
self.assertIs(field_dict["name"], form_declared_fields["name"])
def test_empty_fields_on_modelform(self): def test_empty_fields_on_modelform(self):
""" """
No fields on a ModelForm should actually result in no fields. No fields on a ModelForm should actually result in no fields.