Django 1.7,localflavor 1.1 USStateSelect()

时间:2015-04-05 22:27:40

标签: django django-models django-forms django-localflavor

我正在使用Django 1.7并且刚刚安装了localflavor 1.1。现在,在创建默认用户时,我想使用USStateSelect()字段。在创建扩展默认用户的Nurse时,我遇到了各种错误。这是我目前的代码:

models.py:

from localflavor.us.forms import USStateSelect

class MyUser(AbstractBaseUser):
    #Address
    house_number = models.IntegerField(default = 0)
    street = models.CharField(max_length = 30, default = "")
    city = models.CharField(max_length = 20, default = "")
    state = USStateSelect()
    zip_Code = models.IntegerField(default = 0)

class Nurse(MyUser):
    #Other fields

forms.py:

class NurseCreationForm(ModelForm):
password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)

class Meta:
    model = Nurse
    fields = ('email', 'first_name', 'last_name', 'department','state',)

错误:

File "C:\Python34\lib\site-packages\django\forms\models.py", line 294, in
__new__raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (state) specified for  
Nurse

我无法在网上找到任何有效的Django 1.7解决方案。在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您的问题是您尝试在模型中使用表单字段USStateSelect。您应该使用模型字段USStateField。请尝试以下方法:

来自localflavor.us.models的

导入USStateField

class MyUser(AbstractBaseUser):
    #Address
    house_number = models.IntegerField(default = 0)
    street = models.CharField(max_length = 30, default = "")
    city = models.CharField(max_length = 20, default = "")
    state = USStateField()
    zip_Code = models.IntegerField(default = 0)

您必须创建迁移并在更新模型后运行它。