You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/manager.md
+15-1Lines changed: 15 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
# Using the manager
2
2
`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.
3
3
4
-
There's three ways to do this:
4
+
There are four ways to do this:
5
5
6
6
***Inherit your model from `psqlextra.models.PostgresModel`:**
7
7
@@ -40,6 +40,20 @@ There's three ways to do this:
40
40
# not like this:
41
41
MyModel.objects.upsert(..) # error!
42
42
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
+
43
57
## Upserting
44
58
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.
0 commit comments