chi*_*ale 7 django manage.py django-1.5
使用Django 1.5中新的可配置用户模型(1,5,0,'beta',2)我在运行manage.py createsuperuser尝试设置必填字段的外键时遇到此错误:
AttributeError:'NoneType'对象没有属性'_state'
由于我的fixtures/initial_data.yaml具有我的用户模型所需的值,因此工作流程为:
我尝试在导入灯具后创建超级用户,因此这不是由City模型的空数据库表引起的问题.
代码摘录,基于文档:
models.py
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']
Run Code Online (Sandbox Code Playgroud)
如何在必填字段上获取用户模型上的外键?谢谢.
编辑:回溯来自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'
Run Code Online (Sandbox Code Playgroud)
小智 3
不能 100% 确定这会解决这个问题,但只是为了好玩,添加 PermissionsMixin 并从模型中删除 is_superuser 。
也许后台正在进行某种权限检查 - 当然它不应该发生,但请尝试一下。
我的怀疑来自于它试图嗅出它需要应用哪些模型权限的概念。
| 归档时间: |
|
| 查看次数: |
1174 次 |
| 最近记录: |