0

I utilities rest-api in django, and I don't succeed to send a "GET" parameter through ajax:

In rest-api app in django I have in the urls.py:

urlpatterns = patterns('',
    url(r'^titles/(?P<author_id>\d+)/$', login_required(views.TitlesViewSet.as_view()) ),
)

In views.py I wrote:

class TitlesViewSetViewSet(ListCreateAPIView):
     serializer_class = TitleSerializer

     def get_queryset(self):
         aouther_id = self.request.GET.get('aouther_id', None)
         return Title.objects.filter(auther = auther_id)

when the code insert to the get_queryset above it doesn't recognize any GET parameter and the aouther_id is set to None.

Does anybody know what I should do?

0

2 Answers 2

2

First, you have a typo in urls, you are using author_id and in view you are trying to get the aouther_id key. Second, you are trying to get the value from the query parameters, but you are not actually using them. Third, you are using named url parameters and those are being stored in the kwargs property of your class based view.

You can access them this way:

class TitlesViewSetViewSet(ListCreateAPIView):
    serializer_class = TitleSerializer

    def get_queryset(self):
        # try printing self.kwargs here, to see the contents
        return Title.objects.filter(author_id=self.kwargs.get('author_id'))
Sign up to request clarification or add additional context in comments.

Comments

1

you should replace a line of the auther_id setting to:

auther_id=self.kwargs['auther_id']

update: I now see jbub answer... thanks man! I just discovered it...

Comments

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.