#23795: Django form fields : limit_choices_to should not be mandatory with 
queryset
-------------------------+-------------------------------------------------
     Reporter:           |      Owner:  nobody
  artscoop               |     Status:  new
         Type:  Bug      |    Version:  1.7
    Component:  Forms    |   Keywords:  formfield, queryset,
     Severity:  Normal   |  limit_choices_to
 Triage Stage:           |  Has patch:  0
  Unreviewed             |      UI/UX:  0
Easy pickings:  0        |
-------------------------+-------------------------------------------------
 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)
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23795>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/051.5e4aa044a4dcb39ffa3b1564b38ca921%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to