Django 1.5可配置用户模型上的“按需外键”字段,createsuperuser表示:AttributeError:';非类型';对象没有属性'_州';
使用Django 1.5(1,5,0,'beta',2)中的新版本,我在尝试设置所需字段的外键时遇到此错误: AttributeError:“非类型”对象没有属性“\u state” 由于我对我的用户模型所需的值有了明确的定义,因此工作流是:Django 1.5可配置用户模型上的“按需外键”字段,createsuperuser表示:AttributeError:';非类型';对象没有属性'_州';,django,manage.py,django-1.5,Django,Manage.py,Django 1.5,使用Django 1.5(1,5,0,'beta',2)中的新版本,我在尝试设置所需字段的外键时遇到此错误: AttributeError:“非类型”对象没有属性“\u state” 由于我对我的用户模型所需的值有了明确的定义,因此工作流是: 创建数据库 python manage.py syncdb 对您刚刚安装Django的auth系统的问题回答“否”,这意味着您没有定义任何超级用户。您想现在创建一个吗?(是/否) 从1个夹具安装了14264个对象 python manage.py crea
class City(models.Model):
city = models.CharField(max_length=70, help_text="City.")
state = models.CharField(max_length=2, help_text="State.")
class Meta:
ordering = ['city']
def __unicode__(self):
return "%s (%s)" % (self.city, self.state)
class PersonaManager(BaseUserManager):
[...]
def create_superuser(self, email, name, birthplace, password):
"""
Creates and saves a superuser with the given email, date of
birth and password.
"""
user = self.create_user(email,
password=password,
name=name,
birthplace=birthplace,
)
user.is_admin = True
user.save(using=self._db)
return user
class Person(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
db_index=True,
)
name = models.CharField(max_length=60)
birthplace = models.ForeignKey('myapp.City', related_name="person_birthplace")
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = PersonaManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['name', 'birthplace']
如何使外键在必填字段中的用户模型上工作?多谢各位
编辑:
从manage.py createsuperuser进行回溯--回溯
:
Traceback (most recent call last):
File "/home/asd/Envs/envdjango15/local/lib/python2.7/site-packages/django/core/management/base.py", line 222, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/asd/Envs/envdjango15/local/lib/python2.7/site-packages/django/core/management/base.py", line 252, in execute
output = self.handle(*args, **options)
File "/home/asd/Envs/envdjango15/local/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 112, in handle
user_data[field_name] = field.clean(raw_value, None)
File "/home/asd/Envs/envdjango15/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 211, in clean
self.validate(value, model_instance)
File "/home/asd/Envs/envdjango15/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1014, in validate
using = router.db_for_read(model_instance.__class__, instance=model_instance)
File "/home/asd/Envs/envdjango15/local/lib/python2.7/site-packages/django/db/utils.py", line 142, in _route_db
return hints['instance']._state.db or DEFAULT_DB_ALIAS
AttributeError: 'NoneType' object has no attribute '_state'
AttributeError: 'NoneType' object has no attribute '_state'
不是100%确定这会解决它,但只是为了好玩,在许可证上钉上一个钉子,然后从你的模型上拔下is_超级用户 也许在后台有某种权限检查的事情在进行——假定它不应该发生,但给它一次机会
我的怀疑来自于it试图嗅出它需要应用什么模型权限的概念。完整的stacktrace在哪里?我已经使用traceback选项执行了createsuperuser:我已经包含了上面的结果。您的pObject中是否使用了多个数据库?不,我使用的是单个数据库。无论如何,只要省略create_superuser定义中的其他必填字段,就可以避免此问题。请检查此线程: