Note in my model contact_email is my primary key for the Contact model
I have an html page and form where users can upload an excel file to upload their contacts to the database. If contact_email has not been uploaded previously, everything works fine, and contacts are uploaded.
However, if the contact_email already exists an error is thrown and contact's info is not updated, for example if in new excel file an existing contact's fav_sport has changed it will not update.
Error given is IntegrityError at /upload/ duplicate key value violates unique constraint "contacts_contact_pkey" DETAIL: Key (contact_email)=(john@gmail.com) already exists.
Here is the code causing the error:
for index, row in df.iterrows():
created = Contact.objects.update_or_create(
contact_name = row[0],
fav_sport = row[1],
contact_email = row[2],
)
How can this code be modified to resolve this error?