Is there any way for using fields value +1 inplace in bulk_create

I believe you could use bulk_create along with F expressions, though I don’t know if this feature is yet available, I only have saw this being discussed on this forum.

EDIT:
It’s indeed not available, found the discussion.


But you can replace your code with get_or_create, that essentially does the same thing. It would look like:

raw_file, created = RawFileReference.objects.get_or_create(
    raw_file_name=image_info["raw_file_name"],
    defaults={"reference_count": 0},
)
raw_file.reference_count += 1
raw_file.save(update_fields=("reference_count",))

Hope this helps.

1 Like