[英]Filtering multiple data in Django
我想根據下面的data
值檢索多個值。 目前我所做的是我一直在使用下面for loop
,但它只檢索一個值,我想根據looping result
進行更新。如果有人能弄清楚我在哪里做錯了,那就太好了。 非常感謝你
gallery_photos table. I want to update `Person tbl path when the value of photos column in gallery_photos and path person table equals
人表
@login_required(login_url='log_permission')
def archive_folder(request):
if request.method == 'POST':
data_id = request.POST.get('id') #single value only example we have: 6
data = gallery_photos.objects.filter(gallery_info_id = idd) # multiple value example we have 3 rows
for sample in data :
viewing= sample.photos # I want to retrieve those three rows based on their photos column value in database
Person.objects.filter(path = viewing).update(path=None)
return JsonResponse({'data': 'success'})
模型.py
class folder_info(models.Model):
title = models.CharField(max_length=50,blank=True, null=True)
date_upload = models.DateField(null=True, blank=True)
class Meta:
db_table = "folder_info"
class gallery_photos(models.Model):
gallery_info_id = models.IntegerField()
photos = models.FileField(upload_to='Photos/', unique=True)
class Meta:
managed = False
db_table = 'gallery_photos'
class Person(models.Model):
path = models.CharField(max_length=60,blank=True, null=True)
你可以這樣試試。 values_list
返回photos
array
,您可以將其用於過濾in
更新所有這些。 print
以進行調試檢查更新前獲得的內容,如果不使用可以刪除。
def archive_folder(request):
if request.method == 'POST':
data_id = request.POST.get('id')
photos_data = gallery_photos.objects.filter(gallery_info_id = data_id).values_list("photos", flat=True)
print(photos_data)
Person.objects.filter(path__in = photos_data).update(path=None)
return JsonResponse({'data': 'success'})
你應該試試django-filter
package。 它支持單個查詢中的多個過濾器。
您還可以使用Imagine 智能編譯器,該編譯器將為您的應用程序生成代碼,包括這個多字段過濾器。 它還將簡化您的權限檢查代碼。 您的 model 非常簡單,您不必做任何自定義操作。 整個代碼庫和測試應該可以使用 Imagine 編譯器生成。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.