Skip to Content
Menú
This question has been flagged
2 Respostes
40614 Vistes

Here is my .py file

class Associateaccounts_respartner(models.Model):
 _inherit = 'res.partner'
@api.multi
 def associate_count():
 # I need to count how many associate account is there based on one particular contact

  associate_count = fields.Integer(compute="associate_count", string='# of Associate', store='true')
 associate_id = fields.One2many('sale.associateaccounts', 'associate_partner_id', string='Associate Accounts')
here is my .xml file
<openerp>    
<data>
<record id="count_associateaccounts_respartner" model="ir.ui.view">
<field name="inherit_id" ref="base.view_partner_form"/>
 <field name="model">res.partner</field>
 <field name="arch" type="xml">
<div name="button_box" position="inside">
 <button class="oe_stat_button" type="action" name="%(associateaccounts.associateaccounts_action)d" attrs="{'invisible': [('customer', '=', False)]}" icon="fa-star">
<field string="Associates" name="associate_count" widget="statinfo"/>
 </button>
</div>
</field>
</record>
</data>
</openerp>
Avatar
Descartar
Best Answer

Hello,

You can have a look at addons/sale_service/models/timesheet.py in Odoo 9.0 to have an example of counting fields & functions. Unless you want to do searches, group bys, etc. the store on the count field is not strictly necessary (but why not, if you ensure that all the dependencies of the computed field are ok).


BTW, usually one2many fields end with 's', so you can distinguish them more easily. For example, "associate_ids" in your case.


I guess your function should look something like:

@api.multi
def associate_account(self):
     for partner in self:
         partner.associate_count = len(partner.associate_ids)

Avatar
Descartar
Autor Best Answer
@api.multidef _associate_count(self): 
 for partner in self:
 partner.associate_count = self.env['sale.associateaccounts'].search_count([('partner_id', '=', partner.id)]) 

associate_count = fields.Integer(compute="_associate_count", string='# of Associate', store=True)

associate_id = fields.One2many('sale.associateaccounts', 'associate_partner_id', string='Associate Accounts')

Thanks for your help but it's not working for me.Instead of that I write down the above code but still no luck.Can anybody help?
Avatar
Descartar
Related Posts Respostes Vistes Activitat
2
d’oct. 23
5436
3
de set. 23
2324
0
de maig 23
2428
1
de maig 23
1884
1
d’abr. 23
1678