2017-07-15 12 views
0

はここ(models.pyに)エラー:ジャンゴ '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に設定する可能性があります。しかしそれはフォームレベルにあり、私の他のフィールドはHTML required属性も失っていると言います。

私はChoiceFieldが常にオプションを選択してからHTML属性「必須」を満たしているように、3つの可能性(「選択」のような「ダミー」デフォルトのオプションは選択されていません)

誰かが別の解決策を知っています(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'}), 
    } 

これは機能しています。

関連する問題