作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用新 Configurable user model从 Django 1.5 (1, 5, 0, 'beta', 2) 运行 manage.py createsuperuser 时出现此错误尝试设置必填字段的外键:
AttributeError: 'NoneType' object has no attribute '_state'
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
:
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% 确定这会解决它,但只是为了踢球,加入 PermissionsMixin 并从模型中删除 is_superuser。
也许在后台进行了某种权限检查——当然它不应该发生,但试一试。
我的怀疑来自于它试图找出它需要应用的模型权限的概念。
关于django - 外键作为 Django 1.5 可配置用户模型上的必填字段,createsuperuser 表示 : AttributeError: 'NoneType' object has no attribute '_state' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13915916/
在使用网站、加载内容等一段时间后,此消息显示“Fill:SelectCommand.Connection 属性尚未初始化”!我认为这是因为 sql 连接,但不确定......我想知道我能做些什么来防止
我是一名优秀的程序员,十分优秀!