type object 'User' has no attribute 'objects django
Asked Answered
S

4

12

I am trying to get list of user from API with JWT token so I generated the token and with email and pass and trying to make get request with token but I get this error:

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'

And I have created custom user as:

views.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

settings.py:

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES':(
    'rest_framework_simplejwt.authentication.JWTAuthentication',
   
),
'DEFAULT_PERMISSION_CLASSES':(
    'rest_framework.permissions.IsAuthenticated',
)
    
}

error

I'm using User.object its working fine but this error comes from JWT.

User models.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)
Stewardess answered 10/9, 2019 at 9:29 Comment(10)
can you add your settings.py?Asben
i have added in editsStewardess
It is .objects not .object.Passageway
then i'm getting this error type object 'User' has no attribute 'objects'Stewardess
maybe it has something to do with custom user modelStewardess
Show us your User model.Staphyloplasty
user model added in editsStewardess
@Stewardess do you have rest_framework.authtoken in your INSTALLED_APPS?Asben
you've misspelled the manager: object = UserManager() should be objectsStaphyloplasty
yes its working nowStewardess
D
40

I just hit the same error with Token not User.

You need to add 'rest_framework.authtoken' to your INSTALLED_APPS And don't forget to manage.py migrate afterward.

That fixed it for me.

Davinadavine answered 25/7, 2020 at 22:17 Comment(0)
J
4

I just got the same error and added the following lines into settings.py file.

SIMPLE_JWT = {
    'ROTATE_REFRESH_TOKENS': False,
    'BLACKLIST_AFTER_ROTATION': False,
    'UPDATE_LAST_LOGIN': False,
}

Alse added the following line into settings.py file.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'corsheaders',
    'rest_framework',
    'rest_framework.authtoken',
    'rest_framework_simplejwt' #new line
]

Also run this command: python manage.py migrate Now, it's working.

Jermaine answered 12/10, 2021 at 6:19 Comment(0)
K
0

I used this and it worked for me

python manage.py migrate

Khalsa answered 24/8, 2021 at 12:8 Comment(1)
The problem was already addressed in the question's comments and your answer is a bit generic to the problem itself.Teacake
R
0

check this code

    objects = MyUserManager()

NO object=...

Ronaldronalda answered 17/12, 2021 at 20:30 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Blameless
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewStrain

© 2022 - 2024 — McMap. All rights reserved.