Skip to content

Commit

Permalink
change to update or create from create
Browse files Browse the repository at this point in the history
  • Loading branch information
ajay-sentry committed Jan 31, 2025
1 parent 7e5a947 commit 4d1167b
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions core/management/commands/insert_data_to_db_from_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 4d1167b

Please sign in to comment.