﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
23795	Django form fields : limit_choices_to should not be mandatory with queryset	artscoop	nobody	"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)
}}}

"	Bug	closed	Forms	1.7	Release blocker	fixed	formfield, queryset, limit_choices_to		Ready for checkin	1	0	0	0	0	0
