Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Context in python code with multiple partner_ids

Johan Vergeer
on 9/4/15, 7:42 AM 500 views

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"
context="{'partner_id': active_id}"
<field name="contractant_count" context="{'partner_id':active_id}"/></div>

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

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.

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?




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

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

    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.

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)


Asked: 9/4/15, 7:42 AM
Seen: 500 times
Last updated: 9/4/15, 10:12 AM