How to catch Traceback
, when inserting a Dataframe
to a Djando model
I have two Django models, one model has data, in this case the Customer
model
Sample models
class Customer(models.Model):
customer_id = models.IntegerField(primary_key=True)
name = models.CharField()
...
class Account(models.Model):
account = models.ForeignKey(Customer, on_delete=models.CASCADE)
description = models.CharField()
...
Account dataframe
try:
df.to_sql('account', con=engine, if_exists='append')
except sqlalchemy.exc.IntegrityError as e:
print(e)
psycopg2.errors.ForeignKeyViolation: insert or update on table
"account" violates foreign key constraint
I want to identify the rows of the dataframe that do not have the Foreign Key, and let the others be inserted