#23795 closed Bug (fixed)
Django form fields : limit_choices_to should not be mandatory with queryset
Reported by: | artscoop | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | 1.7 |
Severity: | Release blocker | Keywords: | formfield, queryset, limit_choices_to |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Hi,
I've found something that might be a bug, and which didn't happen with Django 1.6.
I have an admin definition which uses a custom form field (an ajax field for ForeignKey lookups which inherits from CharField
), and changes the form as follows:
def get_form(self, request, obj=None, **kwargs): form = super(ProfileAdmin, self).get_form(request, obj, **kwargs) if obj is not None: form.base_fields['picture'].queryset = obj.pictures.all() return form
This works well with Django 1.6 (and the queryset filtering is ok), but clashes with Django 1.7 code (*django/forms/models.py, 333, in BaseModelForm.init*)
if hasattr(formfield, 'queryset'): limit_choices_to = formfield.limit_choices_to # this attribute might not be a member of the field 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)
This fails with an AttributeError
because Django assumes, without checking, that when a formfield is altered to use a queryset, it also uses a limit_choices_to
attribute. This is not always true, it seems.
I see no error when instead of the custom field, I use a ModelChoiceField
(this makes sense, ModelChoiceField
always has a limit_choices_to
field).
limit_choices_to = formfield.limit_choices_to
should be
limit_choices_to = getattr(formfield, 'limit_choices_to', None)
Change History (6)
comment:1 by , 10 years ago
comment:2 by , 10 years ago
Has patch: | set |
---|---|
Needs tests: | set |
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 10 years ago
Needs tests: | unset |
---|---|
Severity: | Normal → Release blocker |
PR with tests and docs: https://github.com/django/django/pull/3503
comment:4 by , 10 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
comment:5 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Pull request for django/master made https://github.com/django/django/pull/3497, bug affects 1.7 and above.