[Answered ]-Django auto complete light says field is required on ForeignKey

2👍

Oops !

Product.category is an FK to model Category, but the Autocomplete you are passing (ProductCategoryAutoComplete) is registered for Product model ! Field that should allow selecting a Category should use an autocomplete for Category, not one for Product 😉

Better usage of autocomplete_light.ModelForm

Since you’re using autocomplete_light.ModelForm, you don’t have to specify the field. Just register an autocomplete for Category:

autocomplete_light.register(Category, search_fields=['category_name'])

And let autocomplete_light.ModelForm do the field definition:

class ProductCreateForm(autocomplete_light.ModelForm):
    class Meta:
        model = Product

Yes, that’s all you need 😉

👤jpic

0👍

search_fields is wrong, product does`n have field category_name, if I understand your idea it have to be:

class ProductCategoryAutoComplete(autocomplete_light.AutocompleteModelBase):
    search_fields = ['category__category_name']
    model = Product
    choices = ProductCategory.objects.all()

autocomplete_light.register(Product, ProductCategoryAutoComplete)
👤zymud

Leave a comment