django-autocomplete-light - how to return a different field then a models primary key?

随声附和 提交于 2019-12-04 14:17:44
teconomix

I found the answer.

You actually have to override get_result_value from the base autocomplete.Select2QuerySetView to return the variable you want from the resulting object. :)

However, I still cannot use the autocomplete widget in my crispy forms form - opened a new question for that (see 'list' object has no attribute 'queryset' error when adding a autocomplete field to a model-form)

def get_result_value(self, result):
    """Return the value of a result."""
    return result.pk #change pk to the variable of your choice

My best guess would be that you're making a query out of the KombiPublikationForm class in your forms.py instead of making it from BasePublikation in your models.py, try

class SearchProjectinFormAutocomplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        qs = BasePublikation.objects.filter(typid__in=[222, 223, 224]).filter(zeigen=1)

        if self.q:
            qs = qs.filter(projektnummer__contains=self.q)

        return qs

On the other hand we might want to look on how does the KombiPublikationsTypMedium class look like in order to know how would the query might behave.

Hope this helps!!

--edit--

Try getting rid of the Q statement: qs = qs.filter(projektnummer__contains=self.q)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!