我的models.py如下:
class Prescription(models.Model):
date_prescribed = models.DateField()
doctor = models.ForeignKey(Doctor)
pharmacy = models.ForeignKey(Pharmacy)
class Doctor(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
class Pharmacy(models.Model):
name = models.CharField(max_length=100)
status = models.Charfield(max_length=100)
我想要的是一个查询集,它可以找到过去六个月按月分组的处方数。我使用的是 raw_sql,我的 views.py 如下:
from django.db import connection
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
@api_view(['GET'])
def prescription_trend_overview(request):
query = '''
select concat(year(rx.date_prescribed),'-',month(rx.date_prescribed)) as timeline,
COUNT(rx.id) as total_prescriptions
from myapp_prescription rx, myapp_doctor doctor, myapp_pharmacy pharmacy
where pharmacy.id = rx.pharmacy_id and
doctor.id = rx.doctor_id and
rx.date_prescribed >= '2014-06-04' and
rx.date_prescribed <= '2015-08-15'
group by timeline
order by year(rx.date_prescribed),
month(rx.date_prescribed)
'''
try:
cursor = connection.cursor()
cursor.execute(query)
descr = cursor.description
rows = cursor.fetchall()
result = [dict(zip([column[0] for column in descr], row)) for row in rows]
finally:
cursor.close()
return Response(result, status=status.HTTP_200_OK)
目前开始和结束日期是硬编码的,处方计数适用于所有医生和药房。但是,我现在需要根据 3 个参数过滤结果:
- 医生
- 药店
- 开始日期和结束日期
我正在尝试在 URL 中添加这些参数,例如:
myapp/services/overview?doctor=John&pharmacy=Phizer&start_date=2015-7-28&end_date=2015-2-12
如何捕获这些参数并根据 URL 参数动态更改 SQL 查询?