Situation
I have the following query in Postgres 9.5.
update warehouse_shelf
set package_count = counter.result
from (
select count(id) as result, shelf_id
from warehouse_package
group by shelf_id
) counter
where counter.shelf_id = warehouse_shelf.id;
These are my tables.
warehouse_shelf
+----+------+---------------+
| ID | NAME | PACKAGE_COUNT |
+----+------+---------------+
| 1 | S1 | 3 |
| 2 | S2 | 1 |
| 3 | S3 | 0 |
+----+------+---------------+
warehouse_package
+----+------+---------------+
| ID | NAME | SHELF_ID |
+----+------+---------------+
| 1 | P1 | 1 |
| 2 | P2 | 1 |
| 3 | P3 | 1 |
| 4 | P4 | 2 |
+----+------+---------------+
Question
How to execute the request above each time a separate package is changed (for example, save, delete, create, update, etc.) using the django model?
I want to execute with django queryset, if possible, and not execute it as a raw request.
source
share