I was able to resolve this by overriding `clean()` instead of 
`to_python()`. In there, I've the set correct queryset to use for 
validation.

I still could not explain why `to_python()` is not called.

On Saturday, November 20, 2021 at 8:38:47 AM UTC+8 Earl Lapus wrote:

> Hi,
>
> I have a ModelForm that has a ModelMultipleChoiceField. In the said field, 
> I've set the queryset to none and assigned the FilteredSelectMultiple as 
> the widget for the field.
>
> It looks like this:
>
> class MyForm(forms.ModelForm):
>     myfield = ModelMultipleChoiceField(
>         queryset=SomeModel.objects.none(),
>         widget=admin.widgets.FilteredSelectMultiple('SomeModel', False),
>         required=False
>     )
>
> I added some javascript on the template to dynamically add items to the 
> widget based on a value from another field. That part already works. But, I 
> am running into an error during submit - validation error. An example error 
> I get is: *"Select a valid choice. 4 is not one of the available 
> choices."  *This is expected since I've set the queryset to be empty, so 
> values will match during validation.
>
> Where should I override the validation? I tried creating a 
> ModelMultipleChoiceField child class and implemented the `to_python()` 
> method but it seems like that method is not called. Is there any 
> documentation that can help me arrive at an answer for this?
>
> Cheers,
> Earl
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/fbe2b159-7d3c-4c62-946c-4f662e4cff7en%40googlegroups.com.

Reply via email to