I have several SA models and you need a trick:
class Entry(Base): __tablename__ = 'entry' id = Column(Integer, primary_key=True) title = Column(Unicode(255)) author_id = Column(Integer, ForeignKey('user.id')) date = Column(DateTime) content = Column(Text) author = relationship('User', backref='entries') class User(Base): __tablename__ = 'user' id = Column(Integer, primary_key=True) username = Column(Unicode(255)) ...
As you can see, this is very classic, users record notes ... I need to make some statistics about them (for example, show their records in a week / month ...)
To count records, I added column_property to the user model :
class User(Base): ... entries_count = column_property(select([func.count(Entry.id)]).\ where(Entry.author_id==id))
Let me show you how many posts were written by users. But for some statistics to specify a date range, I will need to dynamically adapt entries_count to add date criteria.
So the question is: how would you handle the date criteria? is column_property the best solution for this kind of need?
Thanks in advance.
source share