Question

I'm using django-autocomplete-light in a django admin application but i cant get choiches correctly filtered for a fk field with limit_choiches_to argument: I still get the entire queryset. here's the code:

# autocomplete_light.py
from django.db.models import Q

import autocomplete_light

from myapp.models import MyClass
from otherapp.models import Deps


class MyClassAutocomplete(autocomplete_light.AutocompleteModelBase):
    """ MyClass autocomplete widget class """
    choiches = MyModels.objects.filter(
            Q(dpt__in=Deps.MAIN_DEPARTMENTS),
            Q(user__is_active=True)
        )
    search_fields = ['^full_name', 'initials']
    attrs = {'placeholder': 'Type a name'}

autocomplete_light.register(MyClass, MyClassAutocomplete)




# admin.py
class SampleModelAdminForm(forms.ModelForm):
    class Meta:
        link_attrs = {'cols': 105, 'rows': 3}
        model = SampleModel

    def __init__(self, *args, **kwargs):
        super(SampleModelAdminForm, self).__init__(
            *args, **kwargs
        )
        self.fields['my_fk'].widget = autocomplete_light.ChoiceWidget(
            'MyClassAutocomplete'
        )

I also tried to override choices_for_request method in AutocompleteModelBase subclass:

def choices_for_request(self):
    return MyModels.objects.filter(
            Q(dpt__in=Deps.MAIN_DEPARTMENTS),
            Q(user__is_active=True)
    )

By this way I have the filtered queryset, but I loose the autocomplete feature (for every word that I type, e.g. 'Es', it starts to show me the choiches from the A letter)

Anybody can help me with that?

thanks

Was it helpful?

Solution

Typo: choiches in

choiches = MyModels.objects.filter(
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top