'I am getting this error Attribute Error and I have checked for the typos but found none
AttributeError at /api-auth/
Got AttributeError when attempting to get a value for field name on serializer UserDetailsSerializer.
The serializer field might be named incorrectly and not match any attribute or key on the QuerySet instance.
Original exception text was: 'QuerySet' object has no attribute 'name'.
my model
from django.db import models
# Create your models here
class UserDetails(models.Model):
name = models.CharField(max_length=100)
password = models.CharField(max_length=100)
virtualId = models.TextField(null=True, blank=True)
photo = models.ImageField(upload_to='uploads/')
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
serializers:
from rest_framework.serializers import ModelSerializer
from .models import UserDetails
class UserDetailsSerializer(ModelSerializer):
class Meta:
model = UserDetails
fields = '__all__'
views :
from rest_framework.response import Response
from rest_framework.decorators import api_view
from .models import UserDetails
from .serializers import UserDetailsSerializer
# Create your views here.
@api_view(['GET'])
def getAllUserdata(request):
user = UserDetails.objects.all()
serializer = UserDetailsSerializer(user)
return Response(serializer.data)
Solution 1:[1]
Since you are passing querysets so you must set many=True in your serializers.
From the official documentation
To serialize a queryset or list of objects instead of a single object instance, you should pass the many=True
api_view(['GET'])
def getAllUserdata(request):
user = UserDetails.objects.all()
serializer = UserDetailsSerializer(user, many=True)
return Response(serializer.data)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
