Reference¶
pgbulk
¶
Bulk Postgres upsert and update functions:
- Use pgbulk.upsert to do an
INSERT ON CONFLICTstatement. - Use pgbulk.update to do a bulk
UPDATEstatement. - Use pgbulk.copy to do a
COPY FROMstatement. - Use pgbulk.aupsert, pgbulk.aupdate, or pgbulk.acopy for async versions.
pgbulk.UpdateField
¶
Bases: str
For expressing an update field as an expression to an upsert operation.
Example:
results = pgbulk.upsert(
MyModel,
[
MyModel(some_int_field=0, some_key="a"),
MyModel(some_int_field=0, some_key="b")
],
["some_key"],
[
pgbulk.UpdateField(
"some_int_field",
expression=models.F('some_int_field') + 1
)
],
)
pgbulk.UpsertResult
¶
Bases: List['Row']
Returned by pgbulk.upsert when the returning argument is provided.
Wraps a list of named tuples where the names correspond to the underlying Django model attribute names.
Also provides properties to access created and updated rows.
pgbulk.acopy
async
¶
acopy(
queryset: QuerySet[_M],
model_objs: Iterable[_M],
copy_fields: UpdateFieldsTypeDef = None,
*,
exclude: Union[List[str], None] = None,
binary: bool = False
) -> None
Asynchronously copy data into a table.
See pgbulk.copy
Note
Like other async Django ORM methods, acopy currently wraps copy in
a sync_to_async wrapper. It does not yet use an asynchronous database
driver but will in the future.
Source code in pgbulk/core.py
pgbulk.aupdate
async
¶
aupdate(
queryset: QuerySet[_M],
model_objs: Iterable[_M],
update_fields: Union[List[str], None] = None,
*,
exclude: Union[List[str], None] = None,
returning: Union[List[str], Literal[True]],
ignore_unchanged: bool = False
) -> List[Row]
aupdate(
queryset: QuerySet[_M],
model_objs: Iterable[_M],
update_fields: Union[List[str], None] = None,
*,
exclude: Union[List[str], None] = None,
returning: Union[List[str], bool] = False,
ignore_unchanged: bool = False
) -> Union[List[Row], None]
Perform an asynchronous bulk update.
See pgbulk.update
Note
Like other async Django ORM methods, aupdate currently wraps update in
a sync_to_async wrapper. It does not yet use an asynchronous database
driver but will in the future.
Source code in pgbulk/core.py
pgbulk.aupsert
async
¶
aupsert(
queryset: QuerySet[_M],
model_objs: Iterable[_M],
unique_fields: List[str],
update_fields: UpdateFieldsTypeDef = None,
*,
exclude: Union[List[str], None] = None,
returning: Union[List[str], Literal[True]],
ignore_unchanged: bool = False
) -> UpsertResult
aupsert(
queryset: QuerySet[_M],
model_objs: Iterable[_M],
unique_fields: List[str],
update_fields: UpdateFieldsTypeDef = None,
*,
exclude: Union[List[str], None] = None,
returning: Union[List[str], bool] = False,
ignore_unchanged: bool = False
) -> Union[UpsertResult, None]
Perform an asynchronous bulk upsert.
See pgbulk.upsert
Note
Like other async Django ORM methods, aupsert currently wraps upsert in
a sync_to_async wrapper. It does not yet use an asynchronous database
driver but will in the future.
Source code in pgbulk/core.py
pgbulk.copy
¶
copy(
queryset: QuerySet[_M],
model_objs: Iterable[_M],
copy_fields: UpdateFieldsTypeDef = None,
*,
exclude: Union[List[str], None] = None,
binary: bool = False
) -> None
Copy data into a table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queryset
|
QuerySet[_M]
|
queryset: A model or a queryset for the table being copied into. |
required |
model_objs
|
Iterable[_M]
|
An iterable of Django models to copy. |
required |
copy_fields
|
UpdateFieldsTypeDef
|
A list of fields on the model objects to copy.
If |
None
|
exclude
|
Union[List[str], None]
|
A list of fields to exclude from the copy. This is useful
when |
None
|
binary
|
bool
|
If True, copy data in binary format. This can yield improved performance for large datasets. |
False
|
Note
Model signals such as post_save are not emitted.
Source code in pgbulk/core.py
pgbulk.update
¶
update(
queryset: QuerySet[_M],
model_objs: Iterable[_M],
update_fields: Union[List[str], None] = None,
*,
exclude: Union[List[str], None] = None,
returning: Union[List[str], Literal[True]],
ignore_unchanged: bool = False
) -> List[Row]
update(
queryset: QuerySet[_M],
model_objs: Iterable[_M],
update_fields: Union[List[str], None] = None,
*,
exclude: Union[List[str], None] = None,
returning: Union[List[str], bool] = False,
ignore_unchanged: bool = False
) -> Union[List[Row], None]
Performs a bulk update.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queryset
|
QuerySet[_M]
|
A model or a queryset for the table being updated. |
required |
model_objs
|
Iterable[_M]
|
Model object values to use for the update. |
required |
update_fields
|
Union[List[str], None]
|
A list of fields on the model objects to update.
If |
None
|
exclude
|
Union[List[str], None]
|
A list of fields to exclude from the update. This is useful
when |
None
|
returning
|
Union[List[str], bool]
|
If True, returns all fields. If a list, only returns fields in the list. If False, do not return results from the upsert. |
False
|
ignore_unchanged
|
bool
|
Ignore unchanged rows in updates. |
False
|
Note
Model signals such as post_save are not emitted.
Returns:
| Type | Description |
|---|---|
Union[List[Row], None]
|
If |
Source code in pgbulk/core.py
pgbulk.upsert
¶
upsert(
queryset: QuerySet[_M],
model_objs: Iterable[_M],
unique_fields: List[str],
update_fields: UpdateFieldsTypeDef = None,
*,
exclude: Union[List[str], None] = None,
returning: Union[List[str], Literal[True]],
ignore_unchanged: bool = False
) -> UpsertResult
upsert(
queryset: QuerySet[_M],
model_objs: Iterable[_M],
unique_fields: List[str],
update_fields: UpdateFieldsTypeDef = None,
*,
exclude: Union[List[str], None] = None,
returning: Union[List[str], bool] = False,
ignore_unchanged: bool = False
) -> Union[UpsertResult, None]
Perform a bulk upsert.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queryset
|
QuerySet[_M]
|
A model or a queryset for the table being upserted. |
required |
model_objs
|
Iterable[_M]
|
An iterable of Django models to upsert. All models in this list will be bulk upserted. |
required |
unique_fields
|
List[str]
|
A list of fields that define the uniqueness of the model. The model must have a unique constraint on these fields. |
required |
update_fields
|
UpdateFieldsTypeDef
|
A list of fields to update whenever objects already exist.
If an empty list is provided, it is equivalent to doing a bulk insert on
the objects that don't exist. If |
None
|
exclude
|
Union[List[str], None]
|
A list of fields to exclude from the upsert. This is useful
when |
None
|
returning
|
Union[List[str], bool]
|
If True, returns all fields. If a list, only returns fields in the list. If False, do not return results from the upsert. |
False
|
ignore_unchanged
|
bool
|
Ignore unchanged rows in updates. |
False
|
Returns:
| Type | Description |
|---|---|
Union[UpsertResult, None]
|
If |
Note
Model signals such as post_save are not emitted.