E
E
Enter_a_nickname2022-02-18 14:27:01
Django
Enter_a_nickname, 2022-02-18 14:27:01

Why does the form with the SelectMultiple widget not work?

I have implemented a form on a page built on a model.

My forms.py file:

class DogRequestsForm(ModelForm):
    class Meta:
        model = DogRequest
        fields = ['id_name', 'Color', 'Dog']
        widgets = {
            'id_name': NumberInput(attrs={
                'class': 'form-control',
                'placeholder': 'id_name'
            }),
            'Color': SelectMultiple(attrs={
                'class': 'form-control',
                'placeholder': 'Color'
            }),
            'Dog': SelectMultiple(attrs={
                'class': 'form-control',
                'placeholder': 'Dog'
            }),
        }


my models.py file:
class DogRequest(models.Model):
    id_name=models.PositiveSmallIntegerField()
    Colors=models.ForeignKey('Color', on_delete=models.CASCADE, blank=True)
    Dogs=models.ManyToManyField('Dog')


According to the django documentation, the ForeignKey and ManyToManyField forms use the SelectMultiple widget that I specified in my forms.py (for dog and color), but when I save the data in the form, I get an error about incorrect filling.
Moreover, if you remove these widgets for these fields, saving works correctly. What is wrong in my code?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question