[Answer]-Which is the best way to update database in python django from the list

1👍

Since you said that your tables are small, you can do the following.
I assume you can do better by using raw queries, but you can consider the following approach (Hope that code contains no errors).

s3_keys = [list of s3 keys]

Now you want to delete all entries that are not in s3 keys, I think this is simple:

File.objects.exclude(id___in=s3_keys).delete()
Node.objects.exclude(id___in=s3_keys).delete()

Now you want to add s3 which are not in the list to the DB:

#Bring all entries and make them to a set:
db_keys = set(File.objects.all().values_list('key',flat=True))
# find the new keys
new_db_keys = set(s3_keys) - db_keys

Now do bulk create:

new_nodes = []
new_files = []
for key in new db_keys:
   new_nodes.append(Node(key=key))
   new_files.append(File(key=key))
Node.objects.bulk_create(new_nodes)
File.objects.bulk_create(new_files)
👤eran

Leave a comment