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

2

5

I am using django-autocomplete-light

in a form for a model I want to use autocomplete on one of its field. the field is not a foreignkey or something, but just a integer field and for autocomplete I would actually like to use the same model then the form I am filling.

The query set from autocomplete however returns the ID and I want to fill the field "projektnummer".

Any clue how I can setup autocomplete so that it returns not the primary key of the model but some other field?

also it seems that I get a wired failure from crispy forms when I use the autocomplete-widget on the integer field.

models.py

class KombiPublikation(models.Model):
    typid = models.ForeignKey('KombiPublikationsTypMedium', verbose_name='Outputtyp', db_column='typid') # publikationstyp.id or publikationstypinfo.typid
    [...]
    projektnummer = models.IntegerField(verbose_name='Projektnr.', default=0, blank=True)
[...]

views.py

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

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

        return qs

forms.py

class KombiPublikationForm(forms.ModelForm):

    class Meta:
        model = KombiPublikation
        #fields = []
        exclude = ['pub_sprache']
        widgets = {
            'typid': autocomplete.ModelSelect2(url='output:typ-autocomplete', forward=['typtyp']),
            'projektnummer': autocomplete.ModelSelect2(url='output:projekt-form-autocomplete'),
        }
Belemnite answered 6/10, 2016 at 15:32 Comment(0)
B
8

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
Belemnite answered 14/10, 2016 at 16:42 Comment(0)
P
0

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)

Phyllis answered 10/10, 2016 at 15:28 Comment(1)
Sorry, my mistake. Base publication is the abstract base class that contains the field. I wanted to shield my question from this unnecessary complexity. Just forgot to rename it accordingly.Belemnite

© 2022 - 2024 — McMap. All rights reserved.