Skip to content
Advertisement

Django Rest Framework – AssertionError Fix your URL conf, or set the `.lookup_field` attribute on the view correctly

I’m trying to return a single object specific to a user (not a queryset), without having to specify an identifier/pk within the requested URL. Each user has an organisation FK.

i.e. http://website/organisation and not http://website/organisation/1

I’m receiving the following error, since it’s expecting this identifier:

AssertionError: Expected view OrganisationDetail to be called with a URL keyword argument named "user__organisation_id". Fix your URL conf, or set the .lookup_field attribute on the view correctly.

How/What do I need to specify when using the RetrieveModelMixin/GenericAPIView so that it returns a singular object linked by a FK?

My view class:

JavaScript

Related URL:

JavaScript

My model:

JavaScript

Advertisement

Answer

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:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement