summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorBaptiste Mispelon <bmispelon@gmail.com>2014-11-12 21:18:11 +0100
committerBaptiste Mispelon <bmispelon@gmail.com>2014-11-12 22:46:00 +0100
commit606c57a132a1e1e680853c014efc41110ee75a80 (patch)
treede1d9fd7067c715924f56f315e8444fbcd6aef70 /django
parent3e0d7de8a6380c5747717cf32166864a22f258d5 (diff)
[1.7.x] 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. Backport of bfb11b95626f39e2f5e18d97d7761c6f93dcc1a9 from master. Conflicts: django/forms/fields.py
Diffstat (limited to 'django')
-rw-r--r--django/forms/fields.py11
-rw-r--r--django/forms/models.py17
2 files changed, 13 insertions, 15 deletions
diff --git a/django/forms/fields.py b/django/forms/fields.py
index 011cb7762b..f686b34a09 100644
--- a/django/forms/fields.py
+++ b/django/forms/fields.py
@@ -171,17 +171,6 @@ class Field(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.
diff --git a/django/forms/models.py b/django/forms/models.py
index 0f21a43655..16cc459175 100644
--- a/django/forms/models.py
+++ b/django/forms/models.py
@@ -329,11 +329,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):
@@ -1118,6 +1116,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