Dragos Neata
Dragos Neata

Reputation: 153

Django - Enter a list of values - ForeignKey

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?

Upvotes: 0

Views: 482

Answers (1)

Italo Lemos
Italo Lemos

Reputation: 1022

M2M uses SelectMultiple instead Select widget

replace

'studio':forms.SelectMultiple(attrs={'class': 'form-control'}),

Upvotes: 1

Related Questions