当我定义AUTH_USER_MODEL

时间:2019-11-28 19:11:21

标签: django django-rest-framework django-rest-framework-jwt

我正在尝试使用JWT实现注销视图。我的用户继承用于从django.contrib.auth.models.User继承,但是我更改为AbstractUser认为这些问题将消失。

当我在settings.py中定义AUTH_USER_MODEL = 'portal.User'并尝试运行./manage.py migration时,出现此错误:

django.db.utils.ProgrammingError: relation "portal_user" does not exist

settings.py

JWT_AUTH = {
    ...
    'JWT_GET_USER_SECRET_KEY': 'portal.models.jwt_get_secret_key',
}

models.py

def jwt_get_secret_key(user_model):
    return user_model.jwt_secret

class User(AbstractUser):
    USERNAME_FIELD = 'username'
    NAME_FIELD = 'name'
    EMAIL_FIELD = 'email'

    name = models.CharField(max_length=150)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    subscriptions = models.ManyToManyField(Subscription,
                                           related_name='user_subscriptions')
    jwt_secret = models.UUIDField(default=uuid.uuid4)
    ...

如果我删除了AUTH_USER_MODEL = 'portal.User',它将返回:

auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'User.groups'.
    HINT: Add or change a related_name argument to the definition for 'User.groups' or 'User.groups'.
auth.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' clashes with reverse accessor for 'User.user_permissions'.
    HINT: Add or change a related_name argument to the definition for 'User.user_permissions' or 'User.user_permissions'.
portal.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'User.groups'.
    HINT: Add or change a related_name argument to the definition for 'User.groups' or 'User.groups'.
portal.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' clashes with reverse accessor for 'User.user_permissions'.
    HINT: Add or change a related_name argument to the definition for 'User.user_permissions' or 'User.user_permissions'.

我试图删除数据库并创建另一个数据库,但是问题仍然存在。

1 个答案:

答案 0 :(得分:0)

我删除了迁移文件夹。我只需要再次创建它,然后在其中放置一个 init .py文件即可。