在Django 1.5可配置的用户模型中,外键作为必填字段,使用createsuperuser命令会报错:AttributeError: 'NoneType' object has no attribute '_state'。

7

使用Django 1.5(1, 5, 0, 'beta', 2)中的新可配置用户模型时,运行manage.py createsuperuser尝试设置所需字段的外键时出现以下错误:

AttributeError: 'NoneType'对象没有属性'_state'

由于我有一个fixtures/initial_data.yaml包含我需要的用户模型值,因此工作流程如下:

  1. 创建数据库
  2. python manage.py syncdb
  3. 回答“no”到问题您刚刚安装了Django的auth系统,这意味着您没有定义任何超级用户。 您现在要创建一个吗? (是/否)
  4. 从1个fixture(s)安装了14264个对象
  5. python manage.py createsuperuser

我尝试在导入固定数据之后创建超级用户,因此这不是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']

我该如何在用户模型的必填字段上使外键起作用?谢谢。
编辑: 从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'

1
完整的堆栈跟踪在哪里? - borges
我已经使用traceback选项执行了createsuperuser命令:我已经在上面包含了执行结果。 - chirale
1
你的项目中使用了多个数据库吗? - OrPo
不,我正在使用单个数据库。不管怎样,在create_superuser定义中省略强制字段可以避免这个问题。 - chirale
2
请查看此线程:https://dev59.com/cmQo5IYBdhLWcg3whPrI - blaze
你有ForeignKey('myapp.City'...ForeignKey(City...不就足够了吗? - Jeewes
1个回答

3

我不能百分之百确定这样做会解决问题,但是可以试试,在你的模型上附加PermissionsMixin并删除is_superuser。

也许有某种权限检查正在后台进行 - 尽管不应该发生但还是试试吧。

我的怀疑来自于它尝试嗅探需要应用的模型权限的概念。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接