Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework - "detail": "Not found."

Hi when doing this request: groups/25010a31-fc5b-47c8-9c5c-d740e5743f52/members/4/ - I get "detail": "Not found"

However, if you look in the queryset I have printed the Groupmember instance and this ends up printing out that particular instance so clearly it exists?

View:

class MemberDetail(mixins.RetrieveModelMixin,
                   mixins.DestroyModelMixin,
                   mixins.UpdateModelMixin,
                   generics.GenericAPIView):
    serializer_class = GroupMembersSerializer
    lookup_field = "user_id"
    lookup_url_kwarg = "uuid"

    def get_queryset(self):
        group = self.kwargs["uuid"]
        user_id = self.kwargs["user_id"]
        print GroupMember.objects.get(group = group, user_id = user_id)
        return GroupMember.objects.get(group = group, user_id = user_id)

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

    def put(self, request, *args, **kwargs):
        return self.update(self, request, *args, **kwargs)

    def delete(self, request, *args, **kwargs):
        return self.destroy(self, request, *args, **kwargs)

urls:

 urlpatterns = [

    url(r'^$', views.GroupList.as_view()),
    url(r'^(?P<uuid>[^/]+)/$', views.GroupDetail.as_view()),
    url(r'^(?P<uuid>[^/]+)/members/$', views.MemberList.as_view()),
    url(r'^(?P<uuid>[^/]+)/members/(?P<user_id>[0-9]+)/$', views.MemberDetail.as_view()),

] 

Any help?

like image 316
Danny Avatar asked Oct 27 '15 16:10

Danny


3 Answers

Firstly, you need to return a queryset in get_queryset() method.

Also, you have incorrectly defined lookup_url_kwarg as uuid. It should infact be user_id as this url kwarg value is used to perform lookup for uuid lookup_field in the queryset returned from the get_queryset() method.

The default value for lookup_url_kwarg if unset is the same value as lookup_field. So, we don't need to define lookup_url_kwarg even. It will be computed from lookup_field.

class MemberDetail(mixins.RetrieveModelMixin,
                   mixins.DestroyModelMixin,
                   mixins.UpdateModelMixin,
                   generics.GenericAPIView):
    serializer_class = GroupMembersSerializer
    lookup_field = "user_id" # no need to define 'lookup_url_kwarg' as both have same value

    def get_queryset(self):
        group = self.kwargs["uuid"]
        return GroupMember.objects.filter(group = group) # return a queryset

In the get_queryset() method, we just filter using the group and not by user_id as this will be performed by DRF itself whenever there is a retrieve request.

Using .get() on a queryset will return an object and not a queryset. To perform filtering based on the value of lookup_field, we need a queryset. Now, .filter() returns a queryset so we used that here.

Note: When you returned GroupMember.objects.filter(group = group, user_id = user_id), the retrieve tried to perform lookup on this returned queryset on the user_id field with its value as lookup_url_kwarg value i.e. user_id=25010a31-fc5b-47c8-9c5c-d740e5743f52. Since no such object exists in that queryset, it returned that error.

like image 84
Rahul Gupta Avatar answered Nov 13 '22 06:11

Rahul Gupta


In my case in my routers.py file I had written it like this

router.register(r'',PersonViewSet)
router.register(r'PersonEmployee',PersonEmployeeViewSet)

so I changed it to

router.register(r'PersonEmployee',PersonEmployeeViewSet)
router.register(r'',PersonViewSet)

It was reading the empty path URL first and throwing detail not found, so I kept router with the empty path at last and it worked fine.

like image 37
Harvindar Singh Garcha Avatar answered Nov 13 '22 08:11

Harvindar Singh Garcha


normally this error appears when you installed Django rest framework datatables, please check the next values on the configuration of Rest Framework on your settings:

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    ),
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
        'rest_framework_datatables.renderers.DatatablesRenderer',
    ),
    'DEFAULT_FILTER_BACKENDS': (
        'rest_framework_datatables.filters.DatatablesFilterBackend',
    ),
    'DEFAULT_PAGINATION_CLASS': 'rest_framework_datatables.pagination.DatatablesPageNumberPagination',
    'PAGE_SIZE': 50,
    'EXCEPTION_HANDLER': 'utils.rest_framework.views.exception_handler'
}
like image 3
direyes Avatar answered Nov 13 '22 08:11

direyes