Custom Mixin for Mulitple Field Object Lookup in Django REST

2018-12-13 Thread Foobar
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`

Re: Custom Mixin for Mulitple Field Object Lookup in Django REST

2018-10-31 Thread Foobar
Great, this worked for me. Thanks Alan. On Tuesday, 30 October 2018 15:44:09 UTC-4, Alan Crosswell wrote: > > Yes, I believe the default search keyword is `search`. It searches for a > keyword match across all the `search_fields`. I believe that's what you > indicated. If you want to search

Re: Custom Mixin for Mulitple Field Object Lookup in Django REST

2018-10-30 Thread Alan Crosswell
Yes, I believe the default search keyword is `search`. It searches for a keyword match across all the `search_fields`. I believe that's what you indicated. If you want to search individual fields with a variety of possible relationships, take a look at `DjangoFilterBackend`. On Tue, Oct 30, 2018

Re: Custom Mixin for Mulitple Field Object Lookup in Django REST

2018-10-30 Thread Asad Habib
Alan, thanks. With this approach do I have to explicitly define URLs in urls.py or are these generated? For the above, would the URL look like the following? localhost/api/units?search=string Where string is the literal string to be searched? If this is the case, how does it know whether I want

Re: Custom Mixin for Mulitple Field Object Lookup in Django REST

2018-10-30 Thread Alan Crosswell
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, )

Re: Custom Mixin for Mulitple Field Object Lookup in Django REST

2018-10-30 Thread Asad Habib
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 wrote: > This seems like you are reinventing

Re: Custom Mixin for Mulitple Field Object Lookup in Django REST

2018-10-30 Thread Foobar
Thanks Jason. Now, the get_object function in class MultipleFieldLookupMixin is actually being called but it's throwing an error as follows: if self.kwargs[field]: # Ignore empty fields. KeyError: 'unit_name' The ViewSet is defined as follows: class UnitViewSet(CountModelMixin,

Re: Custom Mixin for Mulitple Field Object Lookup in Django REST

2018-10-30 Thread Alan Crosswell
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 wrote: > I have the following code which doesn't work: > > In models.py: > > class Unit(models.Model): >unit_name =

Re: Custom Mixin for Mulitple Field Object Lookup in Django REST

2018-10-30 Thread Jason
Due to python's method resolution order, you need to have mixins first in the class inheritance order. Specifically class UnitViewSet(MultipleFieldLookupMixin, viewsets.ReadOnlyModelViewset): ... See

Custom Mixin for Mulitple Field Object Lookup in Django REST

2018-10-29 Thread Foobar
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`