【发布时间】:2021-02-02 22:57:54
【问题描述】:
在我的应用程序中,我试图从 URL 中查询多个参数,如下所示:
/allot-graph/?sc=Infac%20India%20Pvt.ltd&type=FLC
有两个参数sc和type
有时它们也可能是空的
我正在处理单个参数,如下所示:
class AllotmentbyMonth(APIView):
def get(self, request):
q = 0
try:
q = request.GET['sc']
except:
pass
if q == 0:
print("q", q)
dataset = Allotment.objects.all().annotate(
month=TruncMonth('dispatch_date')).values(
'month').annotate(c=Sum('flows__alloted_quantity')).values('month', 'c')
else:
print("q")
client = Client.objects.filter(client_name = q)[0].pk
print("client", client)
dataset = Allotment.objects.filter(sales_order__owner=client).annotate(
month=TruncMonth('dispatch_date')).values(
'month').annotate(c=Sum('flows__alloted_quantity')).values('month', 'c')
date = list(dataset)
months = list()
for i in date:
months.append(i['month'].strftime("%B"))
chart = {'chart': {'type': 'column', 'height': 400},
'title': {'text': 'Allotments by Months'},
'xAxis': {'categories': months},
'yAxis': {"title": {"text": 'Count'}},
'series': [{'name': 'Months', 'data': [{'name': row['month'], 'y': row["c"]} for row in dataset]}]}
print("chart", chart)
return Response(chart)
我知道这不是理想的方法,但这是我的解决方法。我该如何处理过滤器type,因为写太多if-else 似乎不对。
type 的过滤器是这样的:
.filter(flows__kit__kit_type = type)
【问题讨论】:
标签: django django-queryset url-parameters