#34216: django admin when field is autocomplete it overrides ModelForm filter
-------------------------------------+-------------------------------------
               Reporter:  sahaliyev  |          Owner:  nobody
                   Type:  Bug        |         Status:  new
              Component:             |        Version:  4.1
  contrib.admin                      |       Keywords:  autocomplete,
               Severity:  Normal     |  filter
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 having issue on django admin. I want one of my foreign key field to be
 searchable and I achieved that making it autocomplete.


 {{{
 class CollectionAdmin(VersionAdmin, admin.ModelAdmin):
     form = CollectionForm
     autocomplete_fields = ["task"]
 }}}


 I also filter that foreign key in ModelForm.


 {{{
 class CollectionForm(forms.ModelForm):
     class Meta:
         model = Collection
         fields = "__all__"

     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)

         if self.fields.get("task"):
             self.fields["task"].queryset =
 self.fields["task"].queryset.filter(
                 status=TaskStatus.ASSIGNED
             )
 }}}


 When task is not autocomplete field in Collection my filter works as
 expected. However, when task is autocomplete field in Collection, filter
 does not work. Instead of my filter in form, task admin get_queryset
 method is called which is not what I want.

 TaskAdmin get_queryset method just filters by user, however I want more
 filter as you see above, for TaskStatus as well.


 {{{
 def get_queryset(self, request):
     qs = super().get_queryset(request)
     if
 request.user.groups.filter(name=settings.COPYWRITER_GROUP).exists():
         return qs.filter(assigned_to=request.user)
     return qs
 }}}


 Repeating, form init filter works if field is not autocomplete.

 I tried removing task from autocomplete of Collection and it worked. I
 want my form filter not overridden if field is autocomplete.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34216>
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018521f5f028-dfe9ef0b-e934-4db0-96cd-5e854922313e-000000%40eu-central-1.amazonses.com.

Reply via email to