I have a Django application in which I there are a number of projects. Each project can have a number of budgets, and every time a new budget is added to a project, the existing budgets for that project should have their version_number
incremented by one. The 'current' budget should be the budget whose version_number
is 0- so when trying to 'get' the current budget for use elsewhere in the code, I am doing things like:
budgetInstance = Budget.objects.get(project = project, version_number = 0)
However, it seems that somehow, some of the projects have ended up with a number of budgets
whose version_number
is 0, so it appears that some of the projects in the database seem to have several 'current budgets'...
I know that I will need to fix the logic for how the versioning is done, so that no two budgets
can have the same version_number
, but for now, I just want to get the most recent budget
for each project in the database.
Each Budget
object has attributes such as: project_id
, version_number
, id
, presentation_date
, etc.
So I now want to filter the budgets
that have been returned when I filtered by version_number = 0)
by their presentation_date
field. As I understand, I would do this using min()
, but I can't seem to get this working...
I tried following the accepted answer at: Find oldest/youngest datetime object in a list, and ran the following in the shell:
mostRecentBudget = min(date for date in budgets.dates if date < now)
But I got a TypeError
, which said:
TypeError: 'instancemethod' object is not iterable
Why is this? How can I search through the list of Budgets
in a given Project
, for the one with the most recent presentation_date
field value?