3

This answer is 10 years old.

Is there a more efficient way for doing the following in 2020 with PostgreSQL?

for item in item_list:
    e, new = Entry.objects.get_or_create(
        field1 = item.field1,
        field2 = item.field2,
    )

The above approach hits the DB continuously.

Django 2.2 released bulk_update and bulk_create but a bulk_get_or_create doesn't seem to exist?

alias51
  • 8,178
  • 22
  • 94
  • 166
  • 2
    If you have the appropriate unique indexes in place you could pass `ignore_conflicts=True` to `bulk_create`? – Iain Shelvington Aug 03 '20 at 00:05
  • I think that's a good approximation for bulk `update_or_create`. I could try and add some queries to get the list first for `get_or_create`. Is there a DB hit when you tigger a unique exception? – alias51 Aug 03 '20 at 09:19

0 Answers0