When I've done date-related operations I've had to use the python datetime
and calendar modules. They're both in the standard library so you can just
do "import datetime, calendar" at the top of your model or controller. Then
you could do a simple query for all the transactions on that budget in a
particular month. As a second step you could then use these python modules
to determine which row object has the latest date or datetime field.
If you haven't worked with the datetime module much before it's a bit
counter-intuitive at first, but it's powerful. The key is that time *spans*
have to be represented with a datetime.timedelta() object. You can subtract
one datetime object from another using arithmetic operators, but the result
is a .timedelta(). One way to figure out the latest in a series of dates,
then, would be something like this (untested code that might be full of
syntax errors!!!):
#assume you've made a dict of datetimes and id's from the rows returned by
your db query -- let's call it "dateset".
nowtime = datetime.datetime.utcnow()
latest = {}
for i,d in datelist.items():
if (nowtime - d) > latest:
pass
else:
latest = {i:d}
return latest
The dict returned should provide you with the latest datetime (i.e., the
one closest to now) from your query results, along with the id of the row
holding that date. You can then filter your original query to retrieve just
that row.
#it's a good idea to use .utcnow() instead of the simple .now() to avoid
any timezone issues