How can I render a ManyToManyField as checkboxes?

前端 未结 3 795
闹比i
闹比i 2020-12-23 10:17

I\'m making a game link site, where users can post links to their favorite web game. When people post games they are supposed to check what category the game falls into.

3条回答
  •  旧时难觅i
    2020-12-23 10:42

    class GameForm(forms.ModelForm): 
            name = forms.CharField(max_length=15, label='Name') 
            url = forms.URLField(label='URL', initial='http://') 
            cats = forms.ModelMultipleChoiceField(
                queryset=Category.objects.all(),
                widget=forms.CheckboxSelectMultiple,
                required=True)
    
            class Meta: 
                    model = Game 
                    fields = ('name','url','cats')
    

    that should fix your view, but i'm not sure about the admin. still looking... will edit if i find anything.

提交回复
热议问题