0
这里是我的模型(models.py)错误:Django的ChoiceField'对象有没有属性 'use_required_attribute'
class Score(models.Model):
ROUTINETYPE_CHOICE = (
(0, 'R1'),
(1, 'R2'),
(2, 'F'),
)
routineType = models.IntegerField(choices=ROUTINETYPE_CHOICE)
pointA = models.DecimalField(max_digits=3, decimal_places=1)
pointB = models.DecimalField(max_digits=3, decimal_places=1)
pointC = models.DecimalField(max_digits=5, decimal_places=3)
这是我的表格(forms.py)
class ScoreForm(forms.ModelForm):
class Meta:
ROUTINETYPE_CHOICE = (
(0, 'R1'),
(1, 'R2'),
(2, 'F'),
)
model = Score
fields = ('routineType', 'pointA', 'pointB', 'pointC')
widgets = {
'routineType': forms.ChoiceField(choices=ROUTINETYPE_CHOICE),
'pointA': forms.TextInput(attrs={'placeholder': 'xx,xx', 'value': '0'}),
'pointB': forms.TextInput(attrs={'placeholder': 'xx,xx', 'value': '0'}),
'pointC': forms.TextInput(attrs={'placeholder': 'xx,xx', 'value': '0'}),
}
而且我的看法是平常:
def score_create(request):
if request.method == 'POST':
form = ScoreForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/score/')
else:
form = ScoreForm()
context = {'form': form}
return render(request, 'score_create.html', context)
当我试图显示自己的状态,Django的给我这个错误:
'ChoiceField' object has no attribute 'use_required_attribute'
use_required_attribute
是Django 1.10中的新增功能,我可以将它设置为False。但它在Form级别上,它会说我的其他字段也失去了HTML required
属性。
我只有三种可能性(没有选择“dummy”默认选项,比如“choose ...”),所以我的ChoiceField总是选择一个选项,然后满足HTML属性“required”。
有人知道另一个解决方案(除了设置use_required_attribute=False
)?
一个字段不是一个小部件。 –