I have the following code which doesn't work:

In models.py:

class Unit(models.Model):
   unit_name = models.CharField(max_length=255)

In views.py

class MultipleFieldLookupMixin(object):"""
Apply this mixin to any view or viewset to get multiple field filtering
based on a `lookup_fields` attribute, instead of the default single field 
filtering.
"""def get_object(self):
    queryset = self.get_queryset()             # Get the base queryset
    queryset = self.filter_queryset(queryset)  # Apply any filter backends
    filter = {}
    for field in self.lookup_fields:
        if self.kwargs[field]: # Ignore empty fields.
            filter[field] = self.kwargs[field]
    obj = get_object_or_404(queryset, **filter)  # Lookup the object
    self.check_object_permissions(self.request, obj)
    return obj    
class UnitViewSet(viewsets.ReadOnlyModelViewSet, MultipleFieldLookupMixin):
serializer_class = UnitSerializer
queryset = Unit.objects.all()
lookup_fields = ('pk', 'unit_name')

In router.py

router.register(r'units', UnitViewSet)

I am able to access units/pk but units/unit_name gives a 404 error. Do I 
have to write a custom URL to make this work? If so, then what is the point 
of using Routers with ViewSets? 


I even tried adding the following directly to urls.py but it didn't make a 
difference (perhaps because this needs to be defined in router.py?)


url(r'^units/(?P<unit_name>[^/.]+)$', views.UnitViewSet, name='unit-detail')


Any help would be appreciated. Thanks.

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to