0

For a M2O relation what field should I be using in forms?

models.py

class Studio(models.Model):
    name = models.SlugField(max_length=100)
    rating = models.CharField(max_length=10, default=None)

    def __str__(self):
        return self.name

class AnimeDetail(models.Model):
    title_japanese = models.CharField(max_length=250)
    studio = models.ForeignKey(Studio, on_delete=models.CASCADE, default=None)
    ...

forms.py


from .models import AnimeDetail

class AnimeDetailForm(forms.ModelForm):

    class Meta:
        model = AnimeDetail
        fields = ['icon', 'image', 'title_japanese', 'title_english', 'studio', 'genre', 'total_episodes', 'posted_episodes', 'season', 'date_started', 'date_finished', 'status', 'type_anime', 'age', 'source']

        widgets = {
            'title_japanese': forms.TextInput(attrs={'class': 'form-control'}),
            'studio':forms.Select(attrs={'class': 'form-control'}),
            ...
        }

'studio':forms.Select(attrs={'class': 'form-control'}) -> Select doesnt work properly in this situiation but in other project worked without problems.

Error: enter image description here

What is wrong?

Dragos Neata
  • 153
  • 7

1 Answers1

1

M2M uses SelectMultiple instead Select widget

replace

'studio':forms.SelectMultiple(attrs={'class': 'form-control'}),
Italo Lemos
  • 972
  • 1
  • 9
  • 20