【问题标题】:South add model fails for CustomUserCustomUser 的南添加模型失败
【发布时间】:2014-02-01 19:38:01
【问题描述】:

我的项目使用django.contrib.auth.models.User。我想让email字段唯一,所以我定义了CustomUser

class CustomUser(AbstractBaseUser):
    username = models.CharField(_('username'), max_length=30, unique=True)
    email = models.EmailField(_('Email'), max_length=254, unique=True)
    first_name = models.CharField(_('first name'), max_length=30, blank=True)
    last_name = models.CharField(_('last name'), max_length=30, blank=True)
    is_staff = models.BooleanField(_('staff status'), default=False)
    is_active = models.BooleanField(_('active'), default=True)
    date_joined = models.DateTimeField(_('date joined'), default=timezone.now)

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['email']

但是当我运行manage.py schemamigration myapp --auto 时,我得到了这个错误:

AttributeError: Manager isn't available; User has been swapped for 'myapp.models.CustomUser'

我做错了什么?

【问题讨论】:

  • @Mutant 没有帮助。我的 settings.py 包含AUTH_USER_MODEL = 'myapp.CustomUser'。在models.py 文件中我将外键设置为CustomUser,在views.py 和forms.py 中我使用User = get_user_model()
  • @sk1p 我尝试将class Meta: managed = False 添加到CustomUser,但没有任何改变。
  • 在将其更改为此自定义迁移之前,您是否运行过任何迁移?来自 django 文档的一些说明 - docs.djangoproject.com/en/dev/topics/auth/customizing/…“如果您打算设置 AUTH_USER_MODEL,您应该在创建任何迁移或第一次运行 manage.py migrate 之前设置它。”

标签: python django python-2.7 django-south django-users


【解决方案1】:

正如answer 所建议的那样,我必须在代码中的任何地方用我的新CustomUser 替换User。由于我忘记在我的 django-rest-framework API 代码中替换它,South 在schemamigration 命令期间引发了错误。

【讨论】:

    猜你喜欢
    • 2012-07-11
    • 2011-05-09
    • 2014-06-10
    • 1970-01-01
    • 2014-07-16
    • 2021-06-25
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多