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?
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:changed to:
Firstly, you need to return a queryset in
get_queryset()
method.Also, you have incorrectly defined
lookup_url_kwarg
asuuid
. It should infact beuser_id
as this urlkwarg
value is used to perform lookup foruuid
lookup_field
in the queryset returned from theget_queryset()
method.The default value for
lookup_url_kwarg
if unset is the same value aslookup_field
. So, we don't need to definelookup_url_kwarg
even. It will be computed fromlookup_field
.In the
get_queryset()
method, we just filter using thegroup
and not byuser_id
as this will be performed by DRF itself whenever there is aretrieve
request.Using
.get()
on a queryset will return an object and not a queryset. To perform filtering based on the value oflookup_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)
, theretrieve
tried to perform lookup on this returned queryset on theuser_id
field with its value aslookup_url_kwarg
value i.e.user_id=25010a31-fc5b-47c8-9c5c-d740e5743f52
. Since no such object exists in that queryset, it returned that error.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: