1

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

1 Answer 1

2

Typo: choiches in

choiches = MyModels.objects.filter( 
Sign up to request clarification or add additional context in comments.

3 Comments

can you explain a bit more...?
It seems to work in this way: def choices_for_request(self): self.choiches = MyModels.objects.filter( Q(dpt__in=Deps.MAIN_DEPARTMENTS), Q(user__is_active=True) ) return super(MyModels, self).choices_for_request()
It's NOT "choiches", it's "choices" ! Good call @stevejalim

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.