model class update total cal fields all model
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from app import db
|
||||
from datetime import datetime
|
||||
from sqlalchemy import event
|
||||
|
||||
class ManholeExcavationClient(db.Model):
|
||||
__tablename__ = "mh_ex_client"
|
||||
@@ -81,3 +82,18 @@ class ManholeExcavationClient(db.Model):
|
||||
|
||||
def serialize(self):
|
||||
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
|
||||
|
||||
|
||||
# ==========================================
|
||||
# AUTO CALCULATE GRAND TOTAL
|
||||
# ==========================================
|
||||
def calculate_trench_total(mapper, connection, target):
|
||||
total = 0
|
||||
for column in target.__table__.columns:
|
||||
if column.name.endswith("_total"):
|
||||
total += getattr(target, column.name) or 0
|
||||
target.Total = total
|
||||
|
||||
|
||||
event.listen(TrenchExcavation, "before_insert", calculate_trench_total)
|
||||
event.listen(TrenchExcavation, "before_update", calculate_trench_total)
|
||||
Reference in New Issue
Block a user