在Django中处理URL中的多个参数
在我的应用程序中,我尝试从URL查询多个参数,如下所示:在Django中处理URL中的多个参数,django,django-queryset,url-parameters,Django,Django Queryset,Url Parameters,在我的应用程序中,我尝试从URL查询多个参数,如下所示: /allot-graph/?sc=Infac%20India%20Pvt.ltd&type=FLC class AllotmentbyMonth(APIView): def get(self, request): q = 0 try: q = request.GET['sc'] except: pass i
/allot-graph/?sc=Infac%20India%20Pvt.ltd&type=FLC
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)
有两个参数sc
和type
有时它们也可能是空的
我处理的单个参数如下所示:
/allot-graph/?sc=Infac%20India%20Pvt.ltd&type=FLC
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)
我知道这不是理想的方法,但这是我的解决办法。在这种情况下,我如何处理过滤器类型
,因为写入的太多,否则
似乎不正确
类型的过滤器如下所示:
.filter(flows__kit__kit_type = type)
您可以使用.get(…)
方法作为
class AllotmentbyMonth(APIView):
def get(self, request):
qp_sc = request.query_params.get("sc")
qp_type = request.query_params.get("type")
client_qs = Client.objects.all()
if qp_sc:
client_qs = client_qs.filter(some_field_name=qp_sc)
class allocmentbymonth(APIView):
def get(自我,请求):
qp_sc=请求.查询参数.get(“sc”)
qp_type=request.query_params.get(“type”)
client_qs=client.objects.all()
如果qp_sc:
client_qs=client_qs.filter(一些字段\u name=qp\u sc)
因为您使用的是DRF,所以最好使用它而不是请求。获取如果我同时拥有sc
和类型
?