Skip to content

Commit 17fffc6

Browse files
committed
Add docs on postgres_manager context manager
1 parent 5057a63 commit 17fffc6

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

docs/manager.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Using the manager
22
`django-postgres-extra` provides the `psqlextra.manager.PostgresManager` which exposes a lot of functionality. Your model must use this manager in order to use most of this package's functionality.
33

4-
There's three ways to do this:
4+
There are four ways to do this:
55

66
* **Inherit your model from `psqlextra.models.PostgresModel`:**
77

@@ -40,6 +40,20 @@ There's three ways to do this:
4040
# not like this:
4141
MyModel.objects.upsert(..) # error!
4242

43+
* **Use the `psqlextra.util.postgres_manager` on the fly:**
44+
45+
This allows the manager to be used **anywhere** on **any** model, but only within the context. This is especially useful if you want to do upserts into Django's `ManyToManyField`'s generated `through` table:
46+
47+
from django.db import models
48+
from psqlextra.util import postgres_manager
49+
50+
class MyModel(models.Model):
51+
myself = models.ManyToManyField('self')
52+
53+
# within the context, you can access psqlextra features
54+
with postgres_manager(MyModel.myself.through) as manager:
55+
manager.upsert(...)
56+
4357
## Upserting
4458
An "upsert" is an operation where a piece of data is inserted/created if it doesn't exist yet and updated (overwritten) when it already exists. Django has long provided this functionality through [`update_or_create`](https://docs.djangoproject.com/en/1.10/ref/models/querysets/#update-or-create). It does this by first checking whether the record exists and creating it not.
4559

psqlextra/util.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import copy
2-
31
from contextlib import contextmanager
42

53
from .manager import PostgresManager

0 commit comments

Comments
 (0)