Django - 输入值列表 - ManytoManyField

时间:2017-10-31 09:45:02

标签: django model

我将供应商字段设为选择下拉框,因为我希望它以这种方式运行,但会产生错误: Enter a list of values

我的模特

class Product(models.Model):
    name = models.Charfield(max_length=250)
    supplier = models.ManytoManyField(Supplier)

我的表格:

class ProductForm(forms.ModelForm):

    class Meta:
        model = Product
        fields = ['name', 'supplier']
    widgets = {
        'supplier': forms.Select()
    }

def clean_supplier(self):
    return [self.cleaned_data['supplier']]]

显示错误:输入值列表

1 个答案:

答案 0 :(得分:1)

下拉列表不支持多项选择。使用CheckboxSelectMultiple。  

class ProductForm(forms.ModelForm):

class Meta:
    model = Product
    fields = ['name', 'supplier']
widgets = {
    'supplier': forms.CheckboxSelectMultiple()
}

def clean_supplier(self): return [self.cleaned_data['supplier']]]