Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit bdf81a3

Browse files
committed
catch each row that fails
1 parent 635f7f6 commit bdf81a3

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

core/management/commands/insert_data_to_db_from_csv.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,15 @@ def handle(self, *args, **kwargs):
5151
)
5252
continue
5353

54-
Model.objects.update_or_create(
55-
defaults=model_data,
56-
# Specify the fields to identify the record
57-
# For example, if 'id' is the unique field:
58-
id=row.get("id"),
59-
)
60-
self.stdout.write(self.style.SUCCESS(f"Inserted row: {row}"))
54+
try:
55+
Model.objects.update_or_create(
56+
defaults=model_data,
57+
id=row.get("id"),
58+
)
59+
self.stdout.write(self.style.SUCCESS(f"Inserted row: {row}"))
60+
except Exception as e:
61+
self.stdout.write(self.style.ERROR(f"Error inserting row: {e}"))
62+
continue
6163

6264
self.stdout.write(
6365
self.style.SUCCESS(

0 commit comments

Comments
 (0)