I am creating CRUD APIs so in this question i am asking about the Get all 
data of user using below code in view.py file where i am using 
user.objects.all() but it is showing me error(mentioned below): i need 
suggestions or help to solve this i have also created a Manager file for 
this user [ all files are added in this question ]. how can i fetch all the 
user data from data base using query set.

*Error of the Code:*
Internal Server Error: /wijungleapp/user/ Traceback (most recent call 
last): File "C:\Users\Mukul 
Verma\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py",
 
line 55, in inner response = get_response(request) File "C:\Users\Mukul 
Verma\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\base.py",
 
line 197, in _get_response response = wrapped_callback(request, 
*callback_args, **callback_kwargs) File "C:\Users\Mukul 
Verma\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\decorators\csrf.py",
 
line 54, in wrapped_view return view_func(*args, **kwargs) File 
"C:\Users\Mukul 
Verma\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\generic\base.py",
 
line 84, in view return self.dispatch(request, *args, **kwargs) File 
"C:\Users\Mukul 
Verma\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\views.py",
 
line 509, in dispatch response = self.handle_exception(exc) File 
"C:\Users\Mukul 
Verma\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\views.py",
 
line 469, in handle_exception self.raise_uncaught_exception(exc) File 
"C:\Users\Mukul 
Verma\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\views.py",
 
line 480, in raise_uncaught_exception raise exc File "C:\Users\Mukul 
Verma\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\views.py",
 
line 506, in dispatch response = handler(request, *args, **kwargs) File 
"C:\WiJungleEps\wijungleapp\views.py", line 60, in get queryset = 
User.object.all() AttributeError: type object 'User' has no attribute 
'object'

*view.py*
class User(APIView): 
  def get(self, request): 
        queryset = User.objects.all() 
        serializer = UserDetails(queryset, many=True) 
        return Response(serializer.data)

*serializer.py*
class UserDetails(serializers.ModelSerializer): 
           class Meta: 
                model = User 
                fields = ['username', 'password', 'hardware_id', 
'created_by'] 

*User Model:*
from django.db import models 
from django.contrib.auth.models import AbstractBaseUser 
from wijungleapp.Manager import UserManager 

class User(AbstractBaseUser): 
username = models.CharField(max_length=100, verbose_name="username", 
unique=True) 
password = models.CharField(max_length=100) 
hardware_id = models.CharField(max_length=150, null=True) 
created_by = models.DateField(verbose_name="created_by", auto_now_add=True) 
USERNAME_FIELD = "username" 
REQUIRED_FIELDS = ['password', 'hardware_id'] is_active = 
models.BooleanField(default=True) 
is_admin = models.BooleanField(default=False) 
is_role_vendor = models.BooleanField(default=False) 
is_role_customer = models.BooleanField(default=True) 
 
def __str__(self): 
  return self.username 

def has_perm(self, perm, obj=None): "Does the user have a specific 
permission?" 
  # Simplest possible answer: Yes, always 
  return True 
 
def has_module_perms(self, app_label): 
  "Does the user have permissions to view the app `app_label`?" # Simplest 
possible       answer: Yes, always 
  return True 

def is_staff(self): "Is the user a member of staff?" # Simplest possible 
answer: All admins are staff 
  return self.is_admin 

object = UserManager()

*manager.py*
from django.contrib.auth.models import BaseUserManager 

class UserManager(BaseUserManager): 

def create_user(self, username, password=None, password2=None, 
hardware_id=None):   
         if not username: 
                 raise ValueError("UserName is Required !!!") user = 
self.model( username=username, ) user.set_password(password) 
user.save(using=self._db) 
                   return user 

# Create Super User Here 
def create_superuser(self, username, password, hardware_id): 
               user = self.create_user( username=username, 
password=password, hardware_id=hardware_id, ) 
               user.is_admin = True user.save(using=self._db) 

               return user


Link of Stack Overflow i had mentioned here too:
https://stackoverflow.com/questions/72443954/facing-issue-in-queryset-while-creating-crud-api-in-django-for-user-model-inheri

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/96a1cc49-4301-4a16-a525-eae5e537d88cn%40googlegroups.com.

Reply via email to