我使用此查詢patient = Patient.objects.all()匯出 csv 檔案中的所有物件,并使用此查詢匯出 csv 中的Patient.objects.filter(slug=slug)單個物件。主要問題是將單個物件和所有物件一起下載到 csv 檔案中。我希望單物件查詢僅在我進入詳細資訊頁面時運行,所有物件查詢將在我進入串列頁面時運行。這是我的代碼:
這是我的代碼:
模型.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)
視圖.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 回圈取決于 page 。
uj5u.com熱心網友回復:
在呼叫此視圖時,您需要確定您來自哪個頁面。一種方法是在呼叫此視圖時決議 query_params。例如:
詳細資訊頁面中的匯出鏈接:
/patient_name/export-csv-patient-data/?details=True
并在您的 exportcsv 視圖中:
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 中。對于 WSGI 請求:
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
uj5u.com熱心網友回復:
您可以添加另一個路徑來下載所有患者資料,如下所示:
網址.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
視圖.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
uj5u.com熱心網友回復:
最后我找到了最簡單的解決方案之一。我只是在我的 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'),
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/403014.html
標籤:
上一篇:一個困難的物件查詢
