简体   繁体   English

Django Rest 框架,用户模型出现错误

[英]Django Rest Framework, user models getting error

My project is simple blog app where users can post,comment and like posts.我的项目是一个简单的博客应用程序,用户可以在其中发布、评论和喜欢帖子。 I used Django default user registration page, later I add Djoser (third party token authentication for Django rest framework) with custom user model, it result in crash, please look into this I used Django default user registration page, later I add Djoser (third party token authentication for Django rest framework) with custom user model, it result in crash, please look into this

please note that if I avoid custom user for Djoser,my project works fine,ie I am able to register using token and session authentication请注意,如果我避免使用 Djoser 的自定义用户,我的项目工作正常,即我可以使用令牌和 session 身份验证进行注册

FORMS.PY #for Django's default register view FORMS.PY #for Django 的默认寄存器视图

from django import forms
#from django.contrib.auth.models import User
from .models import User
from django.contrib.auth.forms import UserCreationForm

class UserRegisterForm(UserCreationForm):
    email = forms.EmailField()
    class Meta:
        model = User
        fields=['username','email','phone','first_name','last_name','password1','password2']

MODELS.PY # Djoser Custom user model MODELS.PY # Djoser 自定义用户 model

from django.db import models
from django.contrib.auth.models import AbstractUser
from django.conf import settings

class User(AbstractUser):
    email = models.EmailField(verbose_name='email' ,max_length=223,unique=True)
    phone=models.CharField(null=True,max_length=11)
    REQUIRED_FIELDS = ['username','phone','first_name','last_name']
    #USERNAME_FIELD = 'email'

    def get_username(self):
        return self.email

ERROR TRACEBACK错误回溯

ERRORS:
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'.
userapp.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'.
userapp.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'.

You have to add AUTH_USER_MODEL in your settings.py .您必须在settings.py中添加AUTH_USER_MODEL Doing this will allow that your User model by default is changed and to new one.这样做将允许您的User model 默认更改为新用户。 Hence, will work fine:因此,将正常工作:

AUTH_USER_MODEL = 'APPNAME.MODELCLASSNAME'

In your case, AppName is the app inside where your models.py is located.在您的情况下, AppName 是您的 models.py 所在的应用程序。 It could be accounts or sthg like that:它可能是accounts或类似的东西:

AUTH_USER_MODEL = 'accounts.User'

Have a look at this beautiful documentation on customizing authentication User.看看这个关于自定义身份验证用户的漂亮文档。 customize_user_django custom_user_django

*Note: accounts is my guess on your app name which you did not specified, User is sure which you specified in models.py *注意: accounts是我对您未指定的应用名称的猜测, User确定您在 models.py 中指定的名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM