Я пытаюсь отобразить rawqueryset
данные в шаблоне. Однако эти rawqueryset
столбцы не являются фиксированными, их можно изменить. Поскольку столбцы изменяются в диапазоне дат, опубликованных пользователем (запрос).
Например,
{% for foo in monthly_consult_by_dr %}
<p>{{ foo.cnt_???? }}</p>
<p>{{ foo.cnt_???? }}</p>
<p>{{ foo.cnt_???? }}</p>
<p>{{ foo.cnt_???? }}</p>
<p>{{ foo.cnt_???? }}</p>
<p>{{ foo.cnt_???? }}</p>
{% endfor %}
Но я могу выяснить, какой столбец включен в приведенный ниже код.
{% for key in monthly_consult_by_dr.colunms %}
<p>{{ key }}</p>
{% endfor %}
## result
cnt_201812
cnt_201901
cnt_201902
cnt_201903
cnt_201904
cnt_201905
....
это мой views.py
def index_2_consult_detail(request):
.... skip....
start_month = start_month_from_user_post_request
end_month = end_month_from_user_post_request
from dateutil.rrule import rrule, MONTHLY
def months(start_month, end_month):
start = datetime.datetime.strptime(start_month, '%Y-%m-%d')
end = datetime.datetime.strptime(end_month, '%Y-%m-%d')
return [str(d.year)+f"{d.month:02d}" for d in rrule(MONTHLY, dtstart=start, until=end)]
month_list = months(start_month, end_month)
insert_query_1_monthly_consult_by_dr = ''
add_limit_number = len(month_list)
insert_tb_row_start_month = month_list[0]
for i in month_list:
add_select = '''
, SUM(CASE WHEN M.BASE_TM = '{j}' THEN M.CNT ELSE 0 END) AS cnt_{j}
, SUM(CASE WHEN M.BASE_TM = '{j}' AND M.C_TYPE = 'consulting' THEN M.CNT ELSE 0 END) AS consult_total_cnt__{j}
, SUM(CASE WHEN M.BASE_TM = '{j}' AND M.C_TYPE = 'consulting' AND M.CONSULT_TYPE = 2 THEN M.CNT ELSE 0 END) AS consult_now_cnt__{j}
, SUM(CASE WHEN M.BASE_TM = '{j}' AND M.C_TYPE = 'consulting' AND M.CONSULT_TYPE = 1 THEN M.CNT ELSE 0 END) AS consult_reserv_cnt__{j}
, SUM(CASE WHEN M.BASE_TM = '{j}' AND M.C_TYPE = 'noteconsulting' THEN M.CNT ELSE 0 END) AS note_cont__{j}
'''.format(j=i)
insert_query_1_monthly_consult_by_dr += add_select
monthly_consult_by_dr = Consult.objects.raw(
'''
SELECT
consult_id
, CASE WHEN M.dr_id IS NULL THEN 'total' ELSE MAX(dr_id) END AS D_ID
, CASE WHEN M.dr_id IS NULL THEN 'total' ELSE MAX(dr_name) END AS D_NAME
, SUM(M.CNT) AS CNT
, SUM(CASE WHEN M.C_TYPE = 'consulting' THEN M.CNT ELSE 0 END) AS total_consult_total
, SUM(CASE WHEN M.C_TYPE = 'consulting' AND M.CONSULT_TYPE = 2 THEN M.CNT ELSE 0 END) AS total_consult_now
, SUM(CASE WHEN M.C_TYPE = 'consulting' AND M.CONSULT_TYPE = 1 THEN M.CNT ELSE 0 END) AS total_consult_reserv
, SUM(CASE WHEN M.C_TYPE = 'noteconsulting' THEN M.CNT ELSE 0 END) AS total_note
{insert_query_1_monthly_consult_by_dr}
FROM (
SELECT C.C_TYPE
, C.consult_id
, C.CONSULT_TYPE
, MON.BASE_TM
, D.dr_id
, (D.dr_name) AS dr_name
, COUNT(1) AS CNT
FROM doctor D
, (
SELECT 'consulting' AS C_TYPE, CONSULT_TYPE, dr_id, created_at, consult_id
FROM consult
UNION ALL
SELECT 'noteconsulting' AS C_TYPE, 3, dr_id, created_at, note_id
FROM consult_note
) C
, (
SELECT DATE_FORMAT(DATE_ADD(CONCAT('{insert_tb_row_start_month}', '01000000'), INTERVAL (@num:=@num+1) MONTH), '%%Y%%m') AS BASE_TM # change
FROM TB_ROW
, (SELECT @num :=-1) R
LIMIT {add_limit_number} # change
) MON
WHERE D.dr_id = C.dr_id
AND C.created_at BETWEEN CONCAT(MON.BASE_TM, '01', '000000') AND CONCAT(MON.BASE_TM, '31', '000000')
# add more where
GROUP BY MON.BASE_TM, D.dr_id, C.C_TYPE, C.consult_type
) M
GROUP BY M.dr_id WITH ROLLUP;
'''.format(insert_query_1_monthly_consult_by_dr=insert_query_1_monthly_consult_by_dr, insert_tb_row_start_month=insert_tb_row_start_month, add_limit_number=add_limit_number)
)
Как решить эту проблему?