Kevin
Kevin

Reputation: 3239

Python/django - compare list with database rows

I have a database with rows of image locations, I also have a list of image locations which is generated when I run the script. I want to keep the database in sync with the generated list. I would just drop the table, but this table has vote information which I need to keep. If I delete the image, I don't want there to be an entry, but if I add an image I want to be able to keep the votes for all of the other images

example:

[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

I want to compare the list to the database and if there is an added image I want to see the db do the following:

[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

What is a good way to accomplish this?

I have this so far:

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)

Upvotes: 0

Views: 552

Answers (1)

Compadre
Compadre

Reputation: 834

Assuming you have django model VotableImage, you can get list of one of it's fields by calling db_path_list = VotableImage.objects.values_list('Path', flat=True) and then check each value for presence in files list (that you created by script)

Upvotes: 1

Related Questions