gpt4 book ai didi

django - 外键作为 Django 1.5 可配置用户模型上的必填字段,createsuperuser 表示 : AttributeError: 'NoneType' object has no attribute '_state'

转载 作者:行者123 更新时间:2023-12-04 12:42:01 24 4
gpt4 key购买 nike

使用新 Configurable user model从 Django 1.5 (1, 5, 0, 'beta', 2) 运行 manage.py createsuperuser 时出现此错误尝试设置必填字段的外键:

AttributeError: 'NoneType' object has no attribute '_state'



因为我有一个 fixtures/initial_data.yaml使用我的用户模型所需的值,工作流程是:
  • 创建数据库
  • python manage.py syncdb
  • 对您刚刚安装了 Django 的身份验证系统的问题回答“否”,这意味着您没有定义任何 super 用户。您想现在创建一个吗? (是/否)
  • 从 1 个固定装置安装了 14264 个对象
  • python manage.py createsuperuser

  • 我尝试在导入设备后创建 super 用户,因此这不是由 City 模型的空数据库表引起的问题。

    代码摘录,基于 documentation :

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

    最佳答案

    不是 100% 确定这会解决它,但只是为了踢球,加入 PermissionsMixin 并从模型中删除 is_superuser。

    也许在后台进行了某种权限检查——当然它不应该发生,但试一试。

    我的怀疑来自于它试图找出它需要应用的模型权限的概念。

    关于django - 外键作为 Django 1.5 可配置用户模型上的必填字段,createsuperuser 表示 : AttributeError: 'NoneType' object has no attribute '_state' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13915916/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com