[Fixed]-Django Rest Framework โ€“ AssertionError Fix your URL conf, or set the `.lookup_field` attribute on the view correctly

27๐Ÿ‘

โœ…

You need to override get_object(), not get_queryset() for detail views. You still want the permission checking so I suggest going through the source. First remove your get_queryset() method then try this for starters:

# inside OrganisationDetail
queryset = Organisation.objects.all()

def get_object(self):
    queryset = self.filter_queryset(self.get_queryset())
    # make sure to catch 404's below
    obj = queryset.get(pk=self.request.user.organisation_id)
    self.check_object_permissions(self.request, obj)
    return obj
๐Ÿ‘คmunsu

1๐Ÿ‘

If you are using mixins, change the lookup_field to the right one

class OrganisationDetail(mixins.RetrieveModelMixin, mixins.UpdateModelMixin,generics.GenericAPIView):

    serializer_class = OrganisationDetailSerializer
    lookup_field = 'members' 

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

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

    def patch(self, request, *args, **kwargs):
        return self.partial_update(request, *args, **kwargs)
๐Ÿ‘คKishan K

0๐Ÿ‘

My solution was to change a parameter, it had id, instead of pk in the path that should only show one element, when changing it to <int: pk>, it has worked wonderfully

from the django.urls import path
from rest_framework.urlpatterns import format_suffix_patterns
from posts import views


urlpatterns = [
    path ('posts /', views.PublicationList.as_view ()),
    path ('posts / <int: id> /', views.Publication_detail.as_vi`enter code here`ew ()),
]

urlpatterns = format_suffix_patterns (urlpatterns)
๐Ÿ‘คAllisLove

Leave a comment