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:
class OrganisationDetail( mixins.RetrieveModelMixin, mixins.UpdateModelMixin,generics.GenericAPIView ): serializer_class = OrganisationDetailSerializer lookup_field = 'pk' # I know this is the default and there's no need to specify def get_queryset(self): return Organisation.objects.filter(pk=self.request.user.organisation_id) def get(self, request, *args, **kwargs): return self.retrieve(request, *args, **kwargs) def put(self, request, *args, **kwargs): return self.update(request, *args, **kwargs)
Related URL:
url(r'^api/v1/organisation/$', OrganisationDetail.as_view()),
My model:
class Person(AbstractUser): organisation = models.ForeignKey( Organisation, related_name='members', null=True ) is_admin = models.BooleanField(default=False) def __str__(self): return self.first_name + " " + self.last_name + " - " + self.email
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:
# 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