簡體   English   中英

過濾 Django 中的多個數據

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM