Skip to content

Commit 61e2e3a

Browse files
committed
Add short-hand "bulk_upsert"
1 parent 5351fe9 commit 61e2e3a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

psqlextra/manager.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,21 @@ def upsert_and_get(self, conflict_target: List, fields: Dict):
253253
self.on_conflict(conflict_target, ConflictAction.UPDATE)
254254
return self.insert_and_get(**fields)
255255

256+
def bulk_upsert(self, conflict_target: List, rows: List[Dict]):
257+
"""Creates a set of new records or updates the existing
258+
ones with the specified data.
259+
260+
Arguments:
261+
conflict_target:
262+
Fields to pass into the ON CONFLICT clause.
263+
264+
rows:
265+
Rows to upsert.
266+
"""
267+
268+
self.on_conflict(conflict_target, ConflictAction.UPDATE)
269+
return self.bulk_insert(rows)
270+
256271
def _build_insert_compiler(self, rows: List[Dict]):
257272
"""Builds the SQL compiler for a insert query.
258273
@@ -484,6 +499,20 @@ def upsert_and_get(self, conflict_target: List, fields: Dict):
484499

485500
return self.get_queryset().upsert_and_get(conflict_target, fields)
486501

502+
def bulk_upsert(self, conflict_target: List, rows: List[Dict]):
503+
"""Creates a set of new records or updates the existing
504+
ones with the specified data.
505+
506+
Arguments:
507+
conflict_target:
508+
Fields to pass into the ON CONFLICT clause.
509+
510+
rows:
511+
Rows to upsert.
512+
"""
513+
514+
return self.get_queryset().bulk_upsert(conflict_target, fields)
515+
487516
@staticmethod
488517
def _on_model_save(sender, **kwargs):
489518
"""When a model gets created or updated."""

0 commit comments

Comments
 (0)