I am making a request which returns serialized data. I and iterating through the data and checking if it exist in the data base. If it exist it is ignored and if it doesn’t exist it is saved. The issue I’m having is that the for loop only saves the first record even though it iterates through all the keys of the data and compares it to what is in the database. My code is as follows:
b = MyModel()
data #The serialized data
existingData = Off.object.filter(...)
for key in data:
if existingData.filter(id_str=key['id_str']).exists():
pass #If I place a print key['id_str'] here, all skipped items are printed.
else:
b.x = key['x']
b.y = key['y']
b.save()
Each time the function runs a counter is placed at the bottom of the else statement after b.save() the counter increases but only the first record that passes the check is save and the rest of the items in data are not saved. Where am I going wrong?
First of all you might be overwriting the same
bobject, instead of creating a new one; I mean you check that objects with someid_strdoesn’t exist, but you just overwriteb‘sxandyattributes. Maybe what you need to do is:Hope this helps 🙂
Cheers!