[PATCH v2 06/13] REST: Stop using ViewSets

Stephen Fincane stephen at that.guru
Fri Nov 25 06:56:36 AEDT 2016


On Thu, 2016-11-24 at 14:03 +1100, Andrew Donnellan wrote:
> 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'])
> 

Good spot. I was actually overriding the wrong function altogether - it
should have been 'get_object' and not 'get_queryset'. Fixed in v3.

Stephen


More information about the Patchwork mailing list