2

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

4
  • welcome to SO! Are you running into an error with the PageView class? If so, please post it here. Commented Aug 9, 2020 at 2:46
  • @abhivemp 404 error not found Commented Aug 9, 2020 at 8:57
  • You spelled class wrong in your views (missing the 'c') Commented Aug 9, 2020 at 15:44
  • @abhivemp bad paste :D Commented Aug 10, 2020 at 7:23

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Still? That means the query is wrong. Alternatively, your urls.py file may be misconfigured
@Abey you also need to import your views for Page view to work in the URL like so: views.PageView

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.