17👍
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.
9👍
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.
- [Django]-Using a string as the argument to a Django filter query
- [Django]-When to create a new app (with startapp) in Django?
- [Django]-Django: Can I create a QueryDict from a dictionary?
3👍
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'
}
- [Django]-Django: How to filter Users that belong to a specific group
- [Django]-Different fields for add and change pages in admin
- [Django]-Django – Highlight Navigation based on current page?
1👍
Just wanted to add. In the case I was looking at the queryset
wasn’t returning a queryset with the expected object, so in terms of what the changes would look like for your question:
class MemberDetail(generics.GenericAPIView):
serializer_class = GroupMembersSerializer
queryset = GroupMember.objects.filter(...)
changed to:
queryset = GroupMember.objects.all()
- [Django]-Django: Difference between using server through manage.py and other servers like gunicorn etc. Which is better?
- [Django]-Calling filter with a variable for field name
- [Django]-Is it possible to generate django models from the database?