我使用此查询patient = Patient.objects.all()
导出 csv 文件中的所有对象,使用此查询Patient.objects.filter(slug=slug)
导出 csv 中的单个对象。它的主要问题是将单个对象和所有对象一起下载到csv文件中。我希望单个对象查询仅在我在详细信息页面上运行时运行,并且所有对象查询将在列表页面上运行时运行。这是我的代码:
这是我的代码:
models.py
class Patient(models.Model):
patient_name = models.CharField(max_length=150)
patient_id = models.CharField(max_length=100,blank=True,null=True)
date_of_birth = models.DateField()
age = models.CharField(max_length=100,blank=True,null=True)
phone = models.CharField(max_length=100)
email = models.EmailField(blank=True,null=True)
slug = models.SlugField(max_length=255,unique=True,blank=True,null=True)
views.py
def exportcsv(request): #I am exporting csv file from this view
response = HttpResponse(content_type='text/csv')
response ['Content-Disposition'] = 'attachment; filename=PatientData'+str(datetime.datetime.now())+'.csv'
writer = csv.writer(response)
writer.writerow(['patient_name'])
all_patient_objects = Patient.objects.all() #exporting all objects
single_patients_objects = Patient.objects.filter(slug=slug) #exporting single objects
for i in single_patients_objects: #want to run this loop only in details page
print(i.patient_name)
writer.writerow([i.patient_name])
for i in all_patient_objects: #want to run this loop only in list page
print(i.patient_name)
writer.writerow([i.patient_name])
return response
#urls.py
path('all-patient/',views.AllPatient,name='all-patient'), #list page
path('<slug:slug>/patient-details/',PatientDetails.as_view(),name='patient-details'), #details page
path('<slug:slug>/export-csv-patient-data/',views.exportcsv,name='export-csv-patient-data'),
现在,它将所有对象和单个对象一起下载到单个csv文件中。我希望它只能从详细信息页面下载单个对象,从列表页面下载所有对象。
我只想在导出单个对象的详细信息页面中运行此循环:
for i in single_patients_objects: #want to run this loop only in details page
print(i.patient_name)
writer.writerow([i.patient_name])
我只想在列表页面中运行此循环以下载所有对象:
for i in all_patient_objects: #want to run this loop only in list page
print(i.patient_name)
writer.writerow([i.patient_name])
基本上我有两个 for 循环,并且一次只想运行一个 for 循环,具体取决于页面。
调用此视图时,您需要确定来自哪个页面。一种方法可能是在调用此视图时解析query_params。例如:
详细信息页面中的导出链接:
/patient_name/export-csv-patient-data/?details=True
并在导出 CSV 视图中:
def exportcsv(request):
details = request.query_params.get('details')
...
if details:
# Do the stuff for the details export
else:
# Do the stuff for the all patients export
上面的代码片段适用于请求。 在 django.restframework 中。对于WSGIRequests:
def exportcsv(request):
details = request.GET.get('details')
...
if details:
# Do the stuff for the details export
else:
# Do the stuff for the all patients export
您可以添加另一个路径来下载所有患者数据,如下所示:
urls.py
path('export-csv-patient-data/',views.exportcsv,name='export-csv-patient-data'),
path('export-csv-patient-data/<slug:patient_slug>/',views.exportcsv,name='export-csv-patient-data')
# I have moved slug parameter to end instead of start as that is the convention and have also gave it a meaningful name
views.py
def exportcsv(request, patient_slug=None):
# rest of the code
params = {'slug': patient_slug} if patient_slug else {}
patient_names = Patient.objects.filter(**params).values('patient_name')
for patient_name in patient_names:
writer.writerow([patient_name['patient_name']])
return Response
最后,我找到了最简单的解决方案之一。我只是在我的 urls.py 中添加了另一个非 slug url 参数。这是我的代码:
patient = Patient.objects.filter(slug=slug) #this query for slug url
all_patient = Patient.objects.all() #this query for non slug url
for i in patient: #this for loop will run for slug url
writer.writerow([i.patient_id,i.patient_name,i.date_of_birth,i.age,i.phone,i.email,i.gender,i.country,i.state,i.city,i.zip_code,i.address])
if slug == None: #this for loop only running for non slug url
for i in all_patient:
writer.writerow([i.patient_id,i.patient_name,i.date_of_birth,i.age,i.phone,i.email,i.gender,i.country,i.state,i.city,i.zip_code,i.address])
uls.py现在只是添加了另一个非 slug URL。
path('export-csv-patient-data/<slug:slug>/',views.exportcsv,name='export-csv-patient-data'),
path('export-csv-all-data/',views.exportcsv,name='export-csv-all-data'),