【发布时间】:2020-03-17 21:31:46
【问题描述】:
我是 Django 新手,我正在尝试通过保留模型中定义的选项来更改字段类型。
这是我的模特
class MyModel(models.Model):
class Type(models.IntegerChoices):
INDIRECT= 0
DIRECT = 1
BOTH = 2
name = models.CharField(max_length=20)
category = models.IntegerField(choices=Type.choices)
price= models.IntegerField()
这是我的表单类
class MyForm(forms.ModelForm):
#category = forms.ChoiceField(widget=forms.RadioSelect())
class Meta:
model = SPA
fields = [ 'name', 'category', 'price' ]
我知道我们可以使用上面的注释行更改字段类型。但它并没有带来模型的选择......
请告知如何实现它。
【问题讨论】:
标签: django python-3.x django-models django-forms