2017-10-15 82 views
0

choiceField我有我的課:Django中找不到我的領域

from django import forms 
from .models import Donator 

class DonatorForm(forms.ModelForm): 
     BLOOD_CHOICES = (('A-','A-'),  ('A+','A+'),  ('B-','B-'),  ('B+','B+'),  ('AB-','AB-'), ('AB+','AB+'), ('O-','O-'),  ('O+','O+'), ('TODOS','TODOS')) 
     SITUATION_CHOICES = (('Sem Problemas','Sem Problemas'), ('Problemas Momentâneos','Problemas Momentâneos'), ('Problemas Graves', 'Problemas Graves')) 

     class Meta: 
      model = Donator 
      fields = ('name', 'age', 'email','phone', forms.ChoiceField(choices = SITUATION_CHOICES, required=True, label = "Situacao do Doador"), 'bloodType', 'observation') 

我收到:

NameError: name 'SITUATION_CHOICES' is not defined

我怎麼能正確地提起我fieldCHoices出現在窗體上的下拉列表?

此外,它已經設置在model。沒辦法從模型本身得到它?

class Donator(models.Model): 
     class Meta: 
      ordering = ('name',) 

     BLOOD_CHOICES = (
      ('A-','A-'),  ('A+','A+'),  ('B-','B-'),  ('B+','B+'),  ('AB-','AB-'), ('AB+','AB+'), ('O-','O-'),  ('O+','O+'), ('TODOS','TODOS') 
    ) 
+0

如果是模型中的一個字段,你不需要做任何形式使用它。你尤其不需要把它放在'fields'元組中;這根本沒有任何意義。只需從那裏刪除它。 –

+0

@DanielRoseman當我打電話給我時,我將如何在視圖上使用它?已經嘗試刪除它,只需將字段名稱放在'field []'中。但是隻顯示字段名稱,而不是dropDown – PlayHardGoPro

回答

0

您以錯誤的方式使用您的元字段。你的選擇必須在模範課外。

BLOOD_CHOICES = (
      ('A-','A-'),  ('A+','A+'),  ('B-','B-'),  ('B+','B+'),  ('AB-','AB-'), ('AB+','AB+'), ('O-','O-'),  ('O+','O+'), ('TODOS','TODOS') 
    ) 
class Donator(models.Model): 
     blood_type = models.CharField(choices=STATUS_CHOICES, default=1) 
     class Meta: 
      ordering = ('name',) 

這樣它應該工作

BLOOD_CHOICES = (('A-','A-'),  ('A+','A+'),  ('B-','B-'),  ('B+','B+'),  ('AB-','AB-'), ('AB+','AB+'), ('O-','O-'),  ('O+','O+'), ('TODOS','TODOS')) 
SITUATION_CHOICES = (('Sem Problemas','Sem Problemas'), ('Problemas Momentâneos','Problemas Momentâneos'), ('Problemas Graves', 'Problemas Graves')) 
class DonatorForm(forms.ModelForm): 

      choiceFieldName = forms.ChoiceField(choices = SITUATION_CHOICES, required=True, label = "Situacao do Doador"), 'bloodType', 'observation') 
      class Meta: 
       model = Donator 
       fields = ('name', 'age', 'email','phone','choiceFieldName')