Python/Django将列表与数据库行进行比较

2024-10-02 08:21:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含图像位置行的数据库,还有一个运行脚本时生成的图像位置列表。我想保持数据库与生成的列表同步。我只想删除表,但是这个表有我需要保留的投票信息。如果我删除图像,我不希望有一个条目,但如果我添加一个图像,我希望能够保留所有其他图像的投票

示例:

[db]
Name   | Path               | vote_count
image1 | path/to/image1.jpg | 1
image2 | path/to/image2.jpg | 4
image3 | path/to/image3.jpg | 2

[list]
path/to/image1.jpg
path/to/image2.jpg
path/to/image3.jpg
path/to/image4.jpg

我想将列表与数据库进行比较,如果有添加的图像,我希望看到数据库执行以下操作:

^{pr2}$

什么是实现这一目标的好方法?在

到目前为止,我有这个:

def ScanImages(request):
    files = []
    fileRoots = []
    for root, directories, filenames in os.walk('/web/static/web/images/'):
        for filename in filenames: 
            files.append(os.path.join(root,filename))
            fileRoots.append(root)

Tags: topath图像数据库列表forrootfiles
1条回答
网友
1楼 · 发布于 2024-10-02 08:21:16

假设您有django模型VotableImage,那么您可以通过调用db_path_list = VotableImage.objects.values_list('Path', flat=True)来获取其中一个字段的列表,然后检查files列表(由脚本创建)中的每个值是否存在

相关问题 更多 >

    热门问题