Lior Alon
Lior Alon

Reputation: 183

sending arguments to django rest framework mixin is not parsed as keyword

I am using Django rest framework. I am testing my method via postman.

I have a view called ActionView, which in I'm using a mixin:

class ActionView(
             mixins.CreateModelMixin,
             mixins.RetrieveModelMixin,
             generics.GenericAPIView):
queryset = Action.objects.all()
serializer_class = ActionSerializer


def post(self, request, *args, **kwargs):
    return self.create(request, *args, **kwargs)

def get(self, request,*args, **kwargs):
    return self.retrieve(request, *args, **kwargs)

The URLs are:

urlpatterns = [
path(r'action/', views.ActionView.as_view()),
path(r'alert/', views.AlertView.as_view()),

]

and they are included in the main router function.

I am trying to send a get request, like this:

http://localhost:8000/actionsAndAlerts/action/?pk=1/ and like this: http://localhost:8000/actionsAndAlerts/action/?1/

but the pk I am sending is not rendered as a keyword at the get method - and so I keep getting this error: AssertionError: Expected view ActionView to be called with a URL keyword argument named "pk". Fix your URL conf, or set the .lookup_field attribute on the view correctly.

I tried adding: @detail_route(url_path='action/(?P[0-9]+)') above the get method but with no success.

any idea how to solve this?

Thanks a lot in advance.

Upvotes: 0

Views: 785

Answers (1)

neverwalkaloner
neverwalkaloner

Reputation: 47364

You dont need ? before url arg, just use http://localhost:8000/actionsAndAlerts/action/1/. But you need to add to urlpattern pk argument (see details here) to make it's possible for view to select correct object to display, change pattern to this:

path('action/<int:pk>', views.ActionView.as_view()),

Upvotes: 1

Related Questions