This question has been flagged

I am creating a smart button that has to display the number of contracts that a customer has.

For this I have used context but when I debug I notice that it runs the code for multiple partners, first for the actual partner_id, then for the administrator and next for the own company.

This is the xml code I have used:

 <button class="oe_inline oe_stat_button" type="action"
  icon="fa-star"
context="{'partner_id': active_id}"
name="%(action_res_partner_contracts)d">
<div><strong>Contractant</strong><br/>
<field name="contractant_count" context="{'partner_id':active_id}"/></div>
</button>

And this is the python code for taking the contract count:

 contractant_count = fields.Integer("Contracts Count", compute="_contracts_count") 

First I tried to work with context to check if the current partner_id is the active_id.
While debugging I notice that it does get the right amount for the contracts, but when it goes to the own company or the admin partner_id (which both have 0 contracts), contract_count goes back to 0

@api.one
def _contracts_count(self):
    if self.id == self.env.context.get("active_id", False):
    res = len(self.contractanten.ids)
    return res

After that I just wanted to check if the contacts count is 0, when the amount is greater then 0, return the amount of contacts.

 @api.one 
def _contracts_count(self):
    if len(self.contractanten.ids) > 0:
    res = len(self.contractanten.ids)
    return res

This also does not seem to work.

Could someone help me figure this out?

Thanks,

Johan    

===================================UPDATE====================================

Someone told me I shouldn't use context. Instead I have used this code.


contracts_count = fields.Integer("Contracts Count", compute="_contracts_count")

@api.one
    def _contracts_count(self)
    res = int(self.env["res.partner.contractanten"].search_count([("partner_id.id", "=", self.id)]))
   return res

This should work better but I still do not get the desired result.

I will keep posting updates.


Avatar
Discard