2017-07-15 88 views
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)?

+0

一個字段不是一個小部件。 –

回答

0

謝謝丹尼爾。這不是一個非常詳細的答案,但是,你是對的。

widgets = { 
    'routineType': forms.Select(attrs={'class': 'form-control col-sm-2'}), 
    'pointA': forms.TextInput(attrs={'class': 'form-control col-sm-2', 'placeholder': 'xx,xx', 'value': '0'}), 
    'pointB': forms.TextInput(attrs={'class': 'form-control col-sm-2', 'placeholder': 'xx,xx', 'value': '0'}), 
    'pointC': forms.TextInput(attrs={'class': 'form-control col-sm-2', 'placeholder': 'xx,xx', 'value': '0'}), 
    } 

它的工作原理!

相關問題