Django rest framework - what url should I provide for generic filters?

I am using django with a rest base and I am trying to use a generic filter. My view looks like this:

class Agents(generics.ListAPIView):
    serializer_class = serializer.AgentSerializer
    model = serializer_class.Meta.model
    filter_backends = (filters.DjangoFilterBackend,)
    queryset = models.Agent.objects.all()
    filter_fields = ('available', 'online', 'agency')

      

and I added the following url:

url('^api/agents/$', api_views.Agents.as_view()),

      

now when i enter urls like these:

api/agents/?online=False
api/agents/?available=True

      

it works and returns correct list based on filters. however, when I try this:

api/agents/123/?online=False

      

I am getting page not found.

reading this document tells me that when I use a generic filter it also works to return a single object and they give the following url as an example: http://example.com/api/products/4675/?category=clothing&max_price = 10.00

but I didn't understand, should I create my own url specifically to get one object by id? or should it happen automatically? should I implement get_queryset () that take parameters from a url and return all objects or a specific object and have two urls that use the same look?

The docs are not very clear.

thank!

+3


source to share


1 answer


What the documentation meant is that if you have a DetailView and set up filters in that view, then you might have a 404 for an existing item if it doesn't match the filter criteria.



Note that you get a detailed view for free if you use ViewSet

+1


source







All Articles