我正在尝试自动完成字段,该字段将查看品牌名称,但会将所选对象的主键作为结果发布。到目前为止,我已经得到了这个,但是它将对象的名称发布到表单中。我该怎么办?
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)
和ModelForm中的那个字段:
brand = forms.ModelChoiceField(queryset = Brands.objects.all(),
label= _('Brand'), widget=al.TextWidget('BrandAutocomplete'))