diff --git a/core/management/commands/insert_data_to_db_from_csv.py b/core/management/commands/insert_data_to_db_from_csv.py index cc88388d0e..8a6dcea30d 100644 --- a/core/management/commands/insert_data_to_db_from_csv.py +++ b/core/management/commands/insert_data_to_db_from_csv.py @@ -51,8 +51,14 @@ def handle(self, *args, **kwargs): ) continue - Model.objects.create(**model_data) - self.stdout.write(self.style.SUCCESS(f"Inserted row: {row}")) + # Use update_or_create + Model.objects.update_or_create( + defaults=model_data, + # Specify the fields to identify the record + # For example, if 'id' is the unique field: + id=row.get("id"), + ) + self.stdout.write(self.style.SUCCESS(f"Processed row: {row}")) self.stdout.write( self.style.SUCCESS(