mirror of
https://github.com/django/django.git
synced 2025-10-30 17:16:10 +00:00
Fixed #23795 -- Fixed a regression in custom form fields
Custom form fields having a `queryset` attribute but no `limit_choices_to` could no longer be used in ModelForms. Refs #2445. Thanks to artscoop for the report.
This commit is contained in:
@@ -181,17 +181,6 @@ class Field(six.with_metaclass(RenameFieldMethods, object)):
|
||||
"""
|
||||
return {}
|
||||
|
||||
def get_limit_choices_to(self):
|
||||
"""
|
||||
Returns ``limit_choices_to`` for this form field.
|
||||
|
||||
If it is a callable, it will be invoked and the result will be
|
||||
returned.
|
||||
"""
|
||||
if callable(self.limit_choices_to):
|
||||
return self.limit_choices_to()
|
||||
return self.limit_choices_to
|
||||
|
||||
def has_changed(self, initial, data):
|
||||
"""
|
||||
Return True if data differs from initial.
|
||||
|
||||
@@ -328,11 +328,9 @@ class BaseModelForm(BaseForm):
|
||||
# Apply ``limit_choices_to`` to each field.
|
||||
for field_name in self.fields:
|
||||
formfield = self.fields[field_name]
|
||||
if hasattr(formfield, 'queryset'):
|
||||
limit_choices_to = formfield.limit_choices_to
|
||||
if hasattr(formfield, 'queryset') and hasattr(formfield, 'get_limit_choices_to'):
|
||||
limit_choices_to = formfield.get_limit_choices_to()
|
||||
if limit_choices_to is not None:
|
||||
if callable(limit_choices_to):
|
||||
limit_choices_to = limit_choices_to()
|
||||
formfield.queryset = formfield.queryset.complex_filter(limit_choices_to)
|
||||
|
||||
def _get_validation_exclusions(self):
|
||||
@@ -1133,6 +1131,17 @@ class ModelChoiceField(ChoiceField):
|
||||
self.choice_cache = None
|
||||
self.to_field_name = to_field_name
|
||||
|
||||
def get_limit_choices_to(self):
|
||||
"""
|
||||
Returns ``limit_choices_to`` for this form field.
|
||||
|
||||
If it is a callable, it will be invoked and the result will be
|
||||
returned.
|
||||
"""
|
||||
if callable(self.limit_choices_to):
|
||||
return self.limit_choices_to()
|
||||
return self.limit_choices_to
|
||||
|
||||
def __deepcopy__(self, memo):
|
||||
result = super(ChoiceField, self).__deepcopy__(memo)
|
||||
# Need to force a new ModelChoiceIterator to be created, bug #11183
|
||||
|
||||
Reference in New Issue
Block a user