How filter by url in django rest framework

Viewed 89

VIEWS

class PageView(viewsets.ModelViewSet):
    serializer_class = postserializer
    # queryset = Page.objects.all()
    def get_queryset(self,request,*args,**kwargs,):
        queryset = Page.objects.all()
        return Page.objects.filter(event=self.kwargs['author'])

URLS

router = routers.DefaultRouter()
router.register('^author/(?P<author>.+)/$', PageView ,basename="author")
router.register(r'authors',views.AuthorView, basename="authors")

urlpatterns = [

    path("", include(router.urls))
]

Model

class Page(models.Model):
    author = models.CharField(max_length=30)
    post = models.TextField()

how to search for particular authors of my post like /author/bob/

i've been trying to follow offical documentation but its unclear to me :\
http://www.tomchristie.com/rest-framework-2-docs/api-guide/filtering

1 Answers

If I am not wrong, you are not searching for and using the queryset function correctly. I believe this should do the fix.

def queryset(self):
   postAuthor = self.kwargs['author']
   return Page.objects.filter(postAuthor)

See if that works.

Related