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
)?
一個字段不是一個小部件。 –