calculating sums of a table field when the values are inherited from another table

17 views
Skip to first unread message

mostwanted

unread,
Apr 11, 2022, 5:56:37 AM4/11/22
to web2py-users
I have a problem that i'm failing to fix, i want to calculate the sum of all premiums for all tables in a client's account. A client is paying  insurance for his kids & his parents, i want to display this information in the view in an html table and be able to calculate his total premium for both his kids and his parents, the problem is when i try to add them up it picks up only 1 value from the children's table which is the value last entered and 1 value in the parents table.

When i try sum_values=db.children.policy_cover.premium.sum() i get an error that:
<class 'AttributeError'> 'Field' object has no attribute 'premium'

TABLES
db.define_table('children_policy',
                Field('policy_name'),
                Field('premium', 'double'),
                format='%(policy_name)s')

db.define_table('client',
                Field('full_names'),
                format='%(full_names)s')

db.define_table('children',
                Field('parent', 'reference client'),
                Field('childs_names'),
                Field('policy_cover', 'reference children_policy')

CONTROLLERS
def index():
    clients=db(db.client).select()
    children=db(db.children).select(db.children.ALL)
    parent=db(db.parent).select(db.parent.ALL)
    return locals()

VIEW
<table>
<th>POLICY OWNER</th>
    <th>CHILD</th>
    <th>POLICY</th>
    <th>PREMIUM</th>
<tr>
{{for c in children:}}
    <td>{{=c.parent.full_names}}</td>
    <td>{{=c.childs_names}}</td>
    <td>{{=c.policy_cover.policy_name}}</td>
    <td>{{=c.policy_cover.premium}}</td>
    </tr>
    {{total=sum(c.policy_cover.premium)}}
{{pass}}
    {{pass}}
                    <tr><td>TOTAL PREMIUM:{{=total}}</td></tr>
    {{pass}}

</table>

Jim S

unread,
Apr 11, 2022, 8:07:06 AM4/11/22
to web2py-users
How about this?

{{total = 0}}
{{for c in children:}}
<tr>
    <td>{{=c.parent.full_names}}</td>
    <td>{{=c.childs_names}}</td>
    <td>{{=c.policy_cover.policy_name}}</td>
    <td>{{=c.policy_cover.premium}}</td>
    {{total +=c.policy_cover.premium}}
</tr>
{{pass}}
<tr>
    <td>
        TOTAL PREMIUM: {{=total}}
    </td>
</tr>


mostwanted

unread,
Apr 11, 2022, 10:57:01 AM4/11/22
to web2py-users
Thank you Jim, works
Reply all
Reply to author
Forward
0 new messages