Sergio Lopez
Sergio Lopez

Reputation: 59

Django rest query

Excuse me, how can I navigate in a list in django with% s, since with a single value it works fine for me, but I want to pass two parameters to my query.

class PruebaSQL(ListAPIView):
    serializer_class = CountSerealizer

    def get_queryset(self):
        num = self.kwargs['num']
        date = self.kwargs['date'] 
        result = (num, date)
        queryset = Sale.objects.raw("SELECT 1 id, id_customer_id, count(DISTINCT id_customer_id) FROM sales_sale where datesale > %s group by id_customer_id having count(id_customer_id) >  %s", [result])
        return queryset

How can I put the positions? or there is another way to pass the data

Upvotes: 0

Views: 50

Answers (1)

chris
chris

Reputation: 2063

Note [result] is identical to [(num, date)], which is a list containing one element (a tuple). Try passing result on its own, without putting it in a list - (num, date) already contains the two elements you need:

queryset = Sale.objects.raw("SELECT 1 id, id_customer_id, count(DISTINCT id_customer_id) FROM sales_sale where datesale > %s group by id_customer_id having count(id_customer_id) >  %s", result)

Upvotes: 2

Related Questions