我有我的班级:
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'未定义
我怎样才能正确提及我的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 :(得分: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' )