Asad:

I believe what you want should work with SearchFilter.  Something like this:

from rest_framework.filters import SearchFilter

class UnitViewSet(viewsets.ReadOnlyModelViewSet):
    serializer_class = UnitSerializer
    queryset = Unit.objects.all()
    filter_backends = (SearchFilter, )
    search_fields = ('pk', 'unit_name')


On Tue, Oct 30, 2018 at 2:05 PM Asad Habib <asadhha...@gmail.com> wrote:

> Alan, appreciate your input. Basically, I want to be able to retrieve
> units based on either a primary key or other attribute. Is this possible
> using filtering if URLs are being defined using routers?
>
> On Tue, Oct 30, 2018 at 8:23 AM Alan Crosswell <a...@columbia.edu> wrote:
>
>> This seems like you are reinventing SearchFilter. Maybe I’m
>> misunderstanding.
>> https://www.django-rest-framework.org/api-guide/filtering/
>>
>> On Mon, Oct 29, 2018 at 2:50 PM Foobar <asadhha...@gmail.com> wrote:
>>
>>> 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.
>>>
>> --
>> Alan Crosswell
>> Associate VP & CTO
>>
>> --
>> 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.
>>
> --
> 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.
>


-- 
Alan Crosswell
Associate VP & CTO

-- 
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