As of Django 4.1, the bulk_create
method supports upserts via update_conflicts
, which is the single query, batch equivalent of update_or_create
:
class Foo(models.Model):
a = models.IntegerField(unique=True)
b = models.IntegerField()
queryset = [Foo(1, 1), Foo(1, 2)]
Foo.objects.bulk_create(
queryset,
update_conflicts=True,
unique_fields=['a'],
update_fields=['b'],
)