3 回答

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個(gè)贊
一旦你運(yùn)行程序,這就是我假設(shè)會(huì)發(fā)生的事情(閱讀 #)
def api_report(request):
params = request.GET
if params["type"] == 'revenue': # False so sql is not made, move to next elif
sql = get_revenue_query(params)
elif params["type"] == 'order_count': # False so sql is not made, move to next elif
sql = get_order_created_count(params)
elif params["type"] == 'product_count': # False so sql is not made, move to next elif
sql = get_product_count(params)
elif params["type"] == 'order_card_created_count': # False so sql is not made, move to next elif
sql = get_order_card_created_count(params)
elif params["type"] == 'product_count': # False so sql is not made, move to next elif
sql = get_product_count(params)
elif params["type"] == 'card': # False so sql is not made, move to next elif
sql = get_card_query(params)
elif params["type"] == 'order_not_card_created_count': # False so sql is not made, move to next elif
sql = get_order_not_card_created_count(params)
elif params["type"] == 'product': # False so sql is not made, move to next elif
get_product_report(request) # P.S There is also a chance that if this is run then sql variable will also not be made!
elif params["type"] == 'order_rate_by_district': # This is also false so code leaves.
sql = get_order_rate_by_district(params)
with connection.cursor() as cursor:
cursor.execute(sql)
rows = cursor.fetchall()
data = []
for row in rows:
data.append(OrderRateDataEntry(row[0], row[1], row[2]))
serializer = OrderRateDataEntrySerializer(data, many=True)
return JsonResponse(serializer.data, safe=False)
pass
# When the code is here it still didn't made variable sql. Thus so will crashes when refere to variable sql as it wasn't yet created
with connection.cursor() as cursor:
cursor.execute(sql) # sql was never made here and thus doesn't exist. Code crashes here.
rows = cursor.fetchall()
data = []
for row in rows:
data.append(TimeSeriesDataEntry(row[0], row[1]))
serializer = TimeSeriesDataEntrySerializer(data, many=True)
return JsonResponse(serializer.data, safe=False)
Maby 在第一個(gè) if 語(yǔ)句之前 make 并清空 sql 變量。(或任何您喜歡的默認(rèn)值)

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超8個(gè)贊
您應(yīng)該重新排列 if 序列以忽略sql
空時(shí)的情況。否則你可以sql = 'some default value'
在它上面添加,但它已經(jīng)很難閱讀了。

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超13個(gè)贊
在我改變了
elif params["type"] == 'product':
get_product_report(request)
到
elif params["type"] == 'product':
return get_product_report(params)
它起作用是因?yàn)?get_product_report 是它自己的函數(shù)所以沒(méi)有任何返回結(jié)果到 param = 'product' 條件所以它是錯(cuò)誤的產(chǎn)品參數(shù)行(返回?zé)o)
添加回答
舉報(bào)