Consider two models:
- Project:
- id: An
AutoField
primary key - budget: A
PositiveIntegerField
- some unrelated fields
- id: An
- Expense
- id: An
AutoField
primary key - amount: A
PositiveIntegerField
- project: A
ForeignKey
toProject
- some unrelated fields
- id: An
For each project, I wish to ensure that the sum of their expenses is less than or equal to the budget at all times.
In SQL terms: SELECT SUM(amount) FROM expense WHERE project_id = ?;
should always be less than or equal SELECT budget FROM project WHERE id = ?;
Is there any way to do this in Django, keeping in mind that multiple people may be accessing the web server and creating Expense
s at the same time?
I am using postgresql as my database backend.
I have tried using select_for_update
but that doesn't prevent INSERT
s from taking place and it doesn't seem to work on aggregations anyway.
I was considering save
ing the Expense
to the database first, then query the total cost and remove the Expense
if it's over-budget but then I would need that code to be outside of a transaction so that other threads can see it and then if the server stops mid-processing, the data could be left in an invalid state.