I basically have the same problem as here. However, I want to implement it in Python 3.9 using the peewee framework.
Here's a simplified excerpt of the models in question:
from datetime import datetime
from peewee import DateTimeField, ForeignkeyField, Model
class System(Model):
...
class CheckResults(Model):
system = ForeignKeyField(System, column_name='system', on_delete='CASCADE')
timestamp = DateTimeField(default=datetime.now)
What I tried so far is:
from peewee import fn
CheckResults.select().join(t2 := CheckResults.select(CheckResults.system, max_timestamp := fn.MAX(CheckResults.timestamp)).group_by(CheckResults.system), on=((t2.system == CheckResults.system) & (t2.timestamp == CheckResults.timestamp)))
Which results in:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'ModelSelect' object has no attribute 'system'
How would I implement such a compound query in peewee?