3

I'm trying to do and autocomplete field which will look through brand names, but will post the chosen object primary key as a result. So far I've got this, but it posts the name of the object to the form. How can I do it?

class BrandAutocomplete(al.AutocompleteModelBase):
    search_fields = ['name']
    order_by = ['name']
    autocomplete_js_attributes={
        'placeholder': 'Enter brand name',
    }
    model = Brands

    def choices_for_request(self):
        self.choices = Brands.objects.filter(company=self.request.user.company)
        return super(BrandAutocomplete, self).choices_for_request()


al.register(BrandAutocomplete)

and that field in ModelForm:

brand = forms.ModelChoiceField(queryset = Brands.objects.all(),
        label= _('Brand'), widget=al.TextWidget('BrandAutocomplete'))
jarkow
  • 113
  • 11

0 Answers0