Result of a Rest Framework ListApiView passing parameter by url

0

I am having problems to show the results of a query, the error that shows me is that it does not find the page (404)

I'm starting with django rest framework and maybe it's very obvious but I have not seen it.

It is that I returned serialized the result of a query where I pass the parameters by the url. (I accept other alternatives, in case I'm focusing badly)

Starting with something simple, such as indicating an agent code in the url and returning the sales of that agent.

urls.py

url(r'^ventas-agente-fechas/(?P<agenteId>)/$', views.VentasAgenteFechas.as_view()),

views.py

class VentasAgenteFechas(generics.ListAPIView):
    serializer_class = VentaAgenteFechaSerializer
    def get_queryset(self):
        agente = self.request.QUERY_PARAMS.get('agenteId', None)
        queryset = Venta.objects.all()
        if agente is not None:
            queryset = queryset.filter(Agente=agente)
        return queryset

serializers.py

class VentaAgenteFechaSerializer(serializers.ModelSerializer):
    class Meta:
        model = Venta
        fields = ('id', 'Agente', 'CdgContrato', 'Cliente', 'FechaVenta', 'Importe', 'PorcentajeComision', 'FormaPago')

If I access link , it tells me that the page does not exist.

    
asked by Cecilio Alonso 03.10.2017 в 13:53
source

1 answer

0

When you add this to a url (?P<agenteId>) that means that you need the url needs that data to pass it as a parameter to a view, so your url would need to be something like this: /comisiones/venta-agente-fechas/1/ , because of the how you have written the regular expression, you need to pass the parameter yes or yes, or does not recognize the url and throws a 404.

Seeing that the parameter is optional, I propose the solution in this way:

urls.py

url(r'^ventas-agente-fechas/$', views.VentasAgenteFechas.as_view()),

Ready, with leaving the url like this, it should work. Any questions, says

    
answered by 03.10.2017 / 18:39
source