1

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

模型.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

表格.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 的有效解决方案。在此先感谢您的帮助!

4

1 回答 1

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)

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

于 2015-04-05T22:52:10.187 回答