2016-07-06 89 views
0

我有一個包含圖像位置行的數據庫,我還有一個當我運行腳本時生成的圖像位置列表。我想保持數據庫與生成的列表同步。我只是放棄桌子,但這張桌子有我需要保留的投票信息。如果我刪除的圖像,我不希望這是一個條目,但如果我添加圖像我希望能夠保持投票的所有其它圖像的Python/django - 與數據庫行進行比較的列表

例如:

[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 

我想將名單與數據庫,如果存在添加的形象我想看到的分貝請執行以下操作:

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

什麼是做到這一點的好辦法?

我有這個至今:

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) 

回答

1

假設你有Django的模型VotableImage,你可以得到它的一個字段列表中通過調用db_path_list = VotableImage.objects.values_list('Path', flat=True),然後檢查files列表中存在的每個值(即創建通過腳本)

相關問題