django - 类型对象 'User' 没有属性 'objects django

标签 django python-3.x django-rest-framework django-rest-framework-jwt

我正在尝试使用 jwt token 从 api 获取用户列表,因此我生成了 token 和电子邮件并通过,并尝试使用 token 发出获取请求,但我得到了这个错误:

File "/home/tboss/Desktop/environment/liveimages/backend/venv/lib/python3.7/site-packages/rest_framework_simplejwt/authentication.py", line 111, in get_user
user = User.objects.get(**{api_settings.USER_ID_FIELD: user_id})
rest_framework.request.WrappedAttributeError: type object 'User' has no attribute 'objects'

我已经创建了自定义用户

View .py:
from users.models import User
from rest_framework import viewsets
from rest_framework.decorators import api_view, permission_classes, authentication_classes  
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.status import  (
HTTP_400_BAD_REQUEST,
HTTP_404_NOT_FOUND,
HTTP_200_OK)
from . import serializers
from rest_framework.response import Response
from rest_framework.authentication import TokenAuthentication, SessionAuthentication



class UserViewSet(viewsets.ModelViewSet):
queryset = User.object.all()
serializer_class = serializers.UserSerializers

设置.py:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES':(
    'rest_framework_simplejwt.authentication.JWTAuthentication',

),
'DEFAULT_PERMISSION_CLASSES':(
    'rest_framework.permissions.IsAuthenticated',
)

}

error

我正在使用 User.object,它工作正常
但是这个错误来自 jwt

用户模型.py:
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
from django.utils import timezone
from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver
from rest_framework.authtoken.models  import Token


class UserManager(BaseUserManager):
def _create_user(self, email, password, is_staff, is_superuser, **extra_fields):
    if not email:
        raise ValueError('user must have email address')
    now = timezone.now()
    email = self.normalize_email(email)
    user = self.model(
        email=email,
        is_staff=is_staff,
        is_active=True,
        is_superuser=is_superuser,
        last_login=now,
        date_joined=now,
        **extra_fields
    )
    user.set_password(password)
    user.save(using=self._db)
    return user
def create_user(self, email, password, **extra_fields):
    return self._create_user(email, password, False, False, **extra_fields)

def create_superuser(self, email, password, **extra_fields):
    user=self._create_user(email, password, True, True, **extra_fields)
    user.save(using=self._db)
    return user

class User(AbstractBaseUser, PermissionsMixin):
   email = models.EmailField(max_length = 100, unique = True)
   First_Name = models.CharField(max_length = 100, null = True, blank = True)
   Last_Name = models.CharField(max_length = 100, null = True, blank = True)
  is_staff = models.BooleanField(default = False)
  is_superuser = models.BooleanField(default = False)
  is_active = models.BooleanField(default = True)
  last_login = models.DateTimeField(null = True, blank = True)
  date_joined = models.DateTimeField(auto_now_add=True)

  USERNAME_FIELD = "email"
  EMAIL_FIELD = "email"
  REQUIRED_FIELD = []

  object = UserManager()

def get_absolute_url(self):
    return "/users/%i/" % (self.pk)

最佳答案

我只是用 Token 而不是 User 遇到了同样的错误。
您需要将“rest_framework.authtoken”添加到您的 INSTALLED_APPS
并且不要忘记之后进行 manage.py migrate。
那为我修好了。

关于django - 类型对象 'User' 没有属性 'objects django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57867859/

相关文章:

javascript - 如何更新Backbone JS模型属性?

django {% if user.groups == 'FC' %} 不起作用

python - 如何比较python中的两个列表列表

python-3.x - Google Cloud Functions-为什么GCF将两个位置参数传递给我的函数?

django - 如何定义 APIView 的 url?

python - 如何使用 Django oAuth 工具包从 Django Rest Framework 获取请求应用程序详细信息

python - 如何解决 django 的 send_mail 无法访问网络的问题?

django - 如何将css应用于Django模板中的三级递归mptt树?

django - 如何在 django tests.py 中创建管理员用户

python - 使用 python 请求获取 CSRF token