[PATCH v2 06/13] REST: Stop using ViewSets
Andrew Donnellan
andrew.donnellan at au1.ibm.com
Thu Nov 24 14:03:18 AEDT 2016
On 20/11/16 03:51, Stephen Finucane wrote:
> +class ProjectMixin(object):
> +
> permission_classes = (PatchworkPermission,)
> serializer_class = ProjectSerializer
>
> - def _handle_linkname(self, pk):
> - '''Make it easy for users to list by project-id or linkname'''
> - qs = self.get_queryset()
> - try:
> - qs.get(id=pk)
> - except (self.serializer_class.Meta.model.DoesNotExist, ValueError):
> - # probably a non-numeric value which means we are going by linkname
> - self.kwargs = {'linkname': pk} # try and lookup by linkname
> - self.lookup_field = 'linkname'
> -
> - def retrieve(self, request, pk=None):
> - self._handle_linkname(pk)
> - return super(ProjectViewSet, self).retrieve(request, pk)
> -
> - def partial_update(self, request, pk=None):
> - self._handle_linkname(pk)
> - return super(ProjectViewSet, self).partial_update(request, pk)
> + def get_queryset(self):
> + query = Project.objects.all()
> +
> + if 'pk' in self.kwargs:
> + try:
> + query.get(id=int(self.kwargs['pk']))
> + except (ValueError, Project.DoesNotExist):
> + query.get(linkname=self.kwargs['pk'])
> +
This should be:
+ if 'pk' in self.kwargs:
+ try:
+ query = query.filter(id=int(self.kwargs['pk']))
+ except (ValueError, Project.DoesNotExist):
+ query = query.filter(linkname=self.kwargs['pk'])
--
Andrew Donnellan OzLabs, ADL Canberra
andrew.donnellan at au1.ibm.com IBM Australia Limited
More information about the Patchwork
mailing list