I am starting to use DAL, but I cannot use the default behavior that set the value to the PKs of the objects in my queryset. So I overriden the 'get_result_value' function to set a custom field as the value of my options.
class CategoryAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
country = Agent.get_user_country(self.request.user)
if not self.request.user.is_authenticated():
return Category.objects.none()
qs = Category.objects.filter(country=country)
if self.q:
qs = qs.filter(full_category__icontains=self.q)
return qs
def get_result_value(self, result):
return result.special_key
My issue is that when I submit I get this ModelChoiceField error:
Select a valid choice. That choice is not one of the available choices.
Here is the Form:
class OnsiteCategoryForm(forms.Form):
category = forms.ModelChoiceField(queryset=Category.objects.all(), required=True,
widget=autocomplete.ModelSelect2(url='category_autocomplete'))
Do you have any idea of what could be causing this error ? Thanks