2017-10-15 61 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')