I'm new to the Django Framework and one thing bothers me.
I want a simple Rest Call:
www.abc.com/users/1/cantonments/1/
If i use 'pk' in the url pattern everything works out of the box (pk, pk1, pk2....). But i have some permission functionality which expects the parameters in kwargs in the form 'upk' and 'cpk' for user and cantonment. So if i change pk to upk everything breaks. Somehow the url needs ONE pk.
This works:
url(r'^users/(?P<pk>[0-9]+)/cantonments/(?P<cpk>[0-9]+)/$',
views.CantonmentDetail.as_view()),
This doesnt:
url(r'^users/(?P<upk>[0-9]+)/cantonments/(?P<cpk>[0-9]+)/$',
views.CantonmentDetail.as_view()),
Is there any way to have an url pattern that does not need one entry with pk?
P.S. The error:
Expected view CantonmentDetail to be called with a URL keyword argument named "pk". Fix your URL conf, or set the `.lookup_field` attribute on the view correctly.
EDIT:
My view is simple:
# Authenticated User can show Cantonment Detail
class CantonmentDetail(generics.RetrieveAPIView):
serializer_class = serializers.CantonmentSerializer
permission_classes = [permissions.IsAuthenticated]
def get_queryset(self):
return Cantonment.objects.filter(pk=self.kwargs['cpk'])
Edit2:
I changed get_queryset to get object and it works.
def get_object(self):
queryset = self.filter_queryset(self.get_queryset())
obj = queryset.get(pk=self.kwargs['cpk'])
return obj
Edit3: Using
lookup_url_kwarg = "cpk"
in the class works as well.