'AssertionError at /posts/ 'PostList' should either include a `queryset` attribute, or override the `get_queryset()` method
ERROR in url==http://127.0.0.1:8000/posts/
D:\Priyanka_Angular1\virtual_env\django-angular\lib\site-packages\rest_framework\views.py in dispatch
response = self.handle_exception(exc)
...
▶ Local vars
D:\Priyanka_Angular1\virtual_env\django-angular\lib\site-packages\rest_framework\views.py in handle_exception
self.raise_uncaught_exception(exc)
...
▶ Local vars
D:\Priyanka_Angular1\virtual_env\django-angular\lib\site-packages\rest_framework\views.py in dispatch
response = handler(request, *args, **kwargs)
...
▶ Local vars
D:\Priyanka_Angular1\virtual_env\django-angular\lib\site-packages\rest_framework\generics.py in get
return self.list(request, *args, **kwargs)
...
▶ Local vars
D:\Priyanka_Angular1\virtual_env\django-angular\lib\site-packages\rest_framework\mixins.py in list
queryset = self.filter_queryset(self.get_queryset())
...
▶ Local vars
D:\Priyanka_Angular1\virtual_env\django-angular\lib\site-packages\rest_framework\generics.py in get_queryset
% self.__class__.__name__
serializer.py
from rest_framework import serializers
from posts.models import Post
class PostSerializer(serializers.HyperlinkedModelSerializer):
author = serializers.Field(source='author.username')
api_url = serializers.SerializerMethodField('get_api_url')
class Meta:
model = Post
fields = ('id', 'title', 'description', 'created_on', 'author', 'url', 'api_url')
read_only_fields = ('id', 'created_on')
def get_api_url(self, obj):
return "#/post/%s" % obj.id
views.py
from django.shortcuts import render
from rest_framework import generics
from posts.models import Post
from posts.serializers import PostSerializer
class PostList(generics.ListCreateAPIView):
"""
List all boards, or create a new board.
"""
model = Post
serializer_class = PostSerializer
class PostDetail(generics.RetrieveUpdateDestroyAPIView):
"""
Retrieve, update or delete a board instance.
"""
model = Post
serializer_class = PostSerializer
Solution 1:[1]
You need to include queryset = Post.objects.all() in your PostList view, as well as in PostDetail.
Every view needs a queryset defined to know what objects to look for. You define the view's queryset by using the queryset attribute (as I suggested) or returning a valid queryset from a get_queryset method.
By the way, you can get rid of the model attribute in your views as they aren't used. That's not the correct way to tell the view what objects to look for.
Solution 2:[2]
Need to just include in views.py and create serializers.py
views.py from autentication.serializers import TestSerializer
authentication_classes = ()
permission_classes = ()
serializer_class = TestSerializer
serializers.py
from rest_framework import serializers
class TestSerializer(serializers.Serializer):
Note : Python 3.8.x with djangorestframework 3.11.x
Solution 3:[3]
Instead of:
model = Post use queryset=Post.objects.all()
It should word perfectly.
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 | lucasnadalutti |
| Solution 2 | Ashok Parmar |
| Solution 3 | coding360 |
