"Select a valid choise. this id is not one of the available choices." when using django FilterSet and autocomplete

I get this error when I want to filter by an item.

"Select a valid choice. 37537 is not one of the available choices."

I think the problem is with the ComparisonFilter class but i can’t figure out what the problem is.

models

class Drug(models.Model):
    name = models.CharField(max_length=255, null=True, blank=True)
    classification_code = models.CharField(max_length=255, null=True, blank=True)

views

class ComparisonFilter(django_filters.FilterSet):
    id = django_filters.ChoiceFilter(
        label=_('Drug'),
        widget=autocomplete.ListSelect2(
            url='comparison-drug-autocomplete',
            attrs={
                'data-placeholder': _('Select drug...'),
                'onChange': "this.form.submit()",
            },
        ),
    )

    class Meta:
        model = Drug
        fields = ['id', ]


class ComparisonView(FilterView, SingleTableView):
    template_name = 'consultations/comparison.html'
    model = Drug
    table_class = ComparisonTable
    table_pagination = {
        "per_page": 50
    }
    filterset_class = ComparisonFilter

    def get_queryset(self):
        return Drug.objects.filter(classification_code__isnull=False)

The view filters drug by the given name and shows that drug in a table.

Source: Python Questions

LEAVE A COMMENT