1

I am using django-autocomplete-light with django_filters. I looked django-filter with django autocomplete-light, but my autocomplete doesn't work.

Models.py:

class ProfessionalDevelopment(models.Model):
   Name = models.CharField("Professional Development", max_length=20,default = "None")
   ProfessionalExperience = models.ManyToManyField(Person, through='PersonToProfessionalDevelopment')

class PersonToProfessionalDevelopment(models.Model):
   PersonID = models.ForeignKey(Person,  on_delete=models.CASCADE)
   ProfID = models.ForeignKey(ProfessionalDevelopment,  on_delete=models.CASCADE)
   Desc = models.CharField("Professional Development Description", max_length=30, default="None")

Views.py:

class ProfessionalDevelopmentAutocomplete(autocomplete.Select2QuerySetView):
 def get_queryset(self):
    qs = ProfessionalDevelopment.objects.all()
    if self.q:
        qs = qs.filter(name__istartswith=self.q)
    return qs

urls.py

urlpatterns = [url(r'^search/ProfessionalDevelopment-autocomplete/$', ProfessionalDevelopmentAutocomplete.as_view(),
    name='ProfessionalDevelopment-autocomplete']

Filters.py:

ProfessionalDevelopment = django_filters.CharFilter(name='professionaldevelopment__Name', lookup_expr='icontains',
                                                    widget=autocomplete.ModelSelect2(url='RSR:ProfessionalDevelopment-autocomplete'))

I got an error says 'list' object has no attribute 'queryset'. Then I changed the code to autocomplete.Select2 instead of autocomplete.ModelSelect2 (according to https://github.com/yourlabs/django-autocomplete-light/issues/763). Although no error, but I can not type in the text input box. Could someone help me? Thanks

Doris
  • 11
  • 4

1 Answers1

3

You wan to filter model ProfessionalDevelopment against it's name. You need to modify in these steps:

  1. Complete str or Unicode on model ProfessionalDevelopment otherwise the option shows on filter will be id.

    class ProfessionalDevelopment(models.Model):
       Name = models.CharField("Professional Development", 
       max_length=20,default = "None")
       ProfessionalExperience = models.ManyToManyField(Person, 
       through='PersonToProfessionalDevelopment')
    
       def __unicode__(self):
           return self.name
    
  2. Change filter field of ProfessionalDevelopment:

    ProfessionalDevelopment = django_filters.ModelChoiceFilter(
        queryset=ProfessionalDevelopment.objects.all(), 
        widget=autocomplete.ModelSelect2(url="yourcompleteurl")
    )
    

these steps should address you problem. Don't forget add {{ form.media }} in your template.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
wllbll
  • 531
  • 5
  • 11
  • 1
    Hi, I followed your code. But it still does not work. The filter becomes a drop down list without any values in it. – Doris Jul 22 '17 at 02:10
  • 1
    @Doris did you add {{ form.media }} in template ? – wllbll Jul 22 '17 at 06:35
  • No, I did not. What is this for? Where should I add in template? Thanks – Doris Jul 22 '17 at 18:15
  • @Doris the {{ form.media }} will tell template to add js and css tags which are necessary, it's mentioned on dal document. – wllbll Jul 23 '17 at 09:32
  • After I add {{form.media}} in template, still doesn't work. The filter in front end is still a box that does not allow me to type. Do you have any suggestions? thanks – Doris Jul 24 '17 at 21:45
  • Hi, I followed the code and it is working but you have a small typo in "django_filters.ModelChoiceField" -> "django_filters.ModelChoiceFilter". I tried to fix it but my change it was less than 6 chars. Thank you – mr antoni Jul 18 '18 at 09:59