I'm using the python's framework pyramid, sqlalchemy for the data and marshmallow to serialize. I'm want to send a sum of value to the client
I have Challenge and player whom use this challenge, each actions of the player is an event and each events has a duration.
For each challenge, i want to send the sum's duration for each player and also send this value only for one player. I try to use hybrid attribute or pre_dump function but i didn't succeeded
First method : with hybrid attribute
__tablename__ = "Challenge" id = Column(Integer, primary_key=True) name = Column(String(255), unique=True, nullable=False) description = Column(TEXT(length=65535)) map_url = Column(String(255)) end_date = Column(DateTime(timezone=False)) alone_only = Column(Integer) level = Column(String(255)) scalling = Column(Integer) draft = Column(Boolean, server_default=text("0")) admin_id = Column(Integer, ForeignKey("User.id")) admin = relationship("User", backref="challenge_manager") event_sum_user = relationship("Events")``` @hybrid_property def event_sum(self): return sum(Events.duration for Events in self.event_sum_user)
But i have sum for all user, not by user or for one user
Second method :with pre_dump method
id = fields.Int() name = fields.Str() description = fields.Str() end_date = fields.DateTime() alone_only = fields.Int() level = fields.Str() scalling = fields.Int() draft = fields.Bool() admin = fields.Nested(UserSchema) admin_id = fields.Int(load_only=True) event_sum = fields.Int(dump_only=True) @pre_dump def get_eventsum(self,data, **kwargs): data["event_sum"] = DBSession.query(func.sum(Events.duration)).filter(Events.challenge_id==data["id"]).filter(Events.user_id==1).first() return data```
With this method, i've have an error TypeError: 'Challenge' object is not subscriptable'
The purpose of this is to send with each challenge the total duration realise by a user or for each user on the challenge : id_user and total duration.
Thanks for your help
Marilyn