[Fixed]-AssertionError at /posts/ 'PostList' should either include a `queryset` attribute, or override the `get_queryset()` method

15👍

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.

0👍

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

0👍

Instead of:

model = Post use queryset=Post.objects.all()

It should word perfectly.

0👍

In my case, I use generics with RetrieveAPIView and my simple mistake

error code

class PizzeriaRetrieveAPIView(generics.RetrieveAPIView):
    lookup_field = "id"
    querysets = Pizzeria.objects.all()
    serializer_class = PizzeriaDetailSerializer

and fixed code, i removed "s" from querysets -> queryset

class PizzeriaRetrieveAPIView(generics.RetrieveAPIView):
    lookup_field = "id"
    queryset = Pizzeria.objects.all()
    serializer_class = PizzeriaDetailSerializer

everything works fine, somethimes we mistake a small changes…

Leave a comment