When storing a computed field it stores 0 in DB, why?

Viewed 84
class ResBuilding(models.Model):
    _name = "res.partner.building"
    _description = "Customer BUilding"
    _order = "id desc"

    region_id = fields.Many2one('res.state.city', string='Region', required=True, ondelete='cascade')
    city_id = fields.Many2one('city.covered.area', string='Area', required=True, ondelete='cascade')
    name = fields.Char(string='Name', required=True, translate=True, copy=False)
    image = fields.Binary(string="Building image")
    latitude = fields.Char(string='Latitude')
    customer_count = fields.Char(string='Customer Count',compute='cust_count' , store=True, default=False)
    longitude = fields.Char(string='Longitude')
    active = fields.Boolean(string='Active', default=True, track_visibility='onchange')
    partner_ids = fields.One2many('res.partner', 'building_id', string='Customer List', readonly=True)

    @api.multi
    def cust_count(self):
        for record in self:
            count = self.env['res.partner'].search_count([('building_id', '=', record.id)])
            record.customer_count = count


    @api.multi
    @api.depends('name', 'city_id')
    def name_get(self):
        result = []
        for route in self:
            name = route.city_id.name + '-' + route.name
            result.append((route.id, name))
        return result

    @api.multi
    def write(self, vals):
        res = super(ResBuilding, self).write(vals)
        print(vals, self.id)
        if vals.get('city_id'):
            customers = self.env['res.partner'].search([('building_id', '=', self.id)])
            for c in customers:
                c.living_area = vals.get('city_id')
        return res

    @api.multi
    @api.depends('latitude', 'longitude')
    def on_change_location(self):
        for li in self:
            if li.latitude and li.longitude:
                self.env.cr.execute("""update res_partner set location_latitude=""" + str(li.latitude) + """,
                                        location_longitude=""" + str(li.longitude) + """where building_id=""" + str(
                    li.id))
        return True

    @api.multi
    def write(self, vals):
        res = super(ResBuilding, self).write(vals)
        self.on_change_region_id()
        return res

    @api.multi
    @api.depends('region_id')
    def on_change_region_id(self):
        for li in self:
            if li.region_id:
                self.env.cr.execute(
                    """update res_partner set city_id=""" + str(li.region_id.id) + """where building_id=""" + str(
                        li.id))
        return True

XML

            <field name="name">res.partner.building.form</field>
            <field name="model">res.partner.building</field>
            <field name="arch" type="xml">
                <form string="Building" >
                  <sheet>
                      <div class="pull-left">
                         <field name="image" widget='image' class="oe_avatar"/>
                      </div>
                        <group> 
                            <group>             
                                <field name="name"  string="Name" />        
                             </group>
                             <group>
                             <div>
                             <p><strong>Location</strong></p>
                             <i class="fa fa-map-marker" aria-hidden="true"></i>
                             <field name="latitude"/>,
                             <field name="longitude"/>
                             </div>
                             </group>
                             <group> 
                                 <field name="region_id"/>
                                 <field name="city_id"/>
                                 <field name="customer_count" force_save="1" />
                             </group>
                        </group>   
                        <notebook>
                        <page name="partners_lists" > 
                            <field name="partner_ids">
                                <tree string="Distribution Customers" decoration-bf="state=='draft'" decoration-info="state=='open'">                  
                                    
                                    <field name="name"/>                    
                                    <field name="mobile"/>
                                    
                                </tree>
                            </field>
                        </page>
                        </notebook>
                    </sheet>
                </form>
            </field>
        </record> 

I want find the customer count in a specific building based on building id. and want The coustomer_count field to be added to the add custom filter

by this code ,i am getting count correctly . But the field customer_count does not appear in custom filter

when i add store=True, the field is coming in custom filter but the count is coming as 0.

1 Answers

Pass default attribute in your field declaration, change type from char to integer and also pass depends to the function like this:

customer_count = fields.Integer(string='Customer Count', compute='cust_count', store=True, default=False)

Note: Default value for filter will be zero but then you can put what ever integer you want to filter on.

Edit:

I have tested with following code at my end, it is working absolutely fine in filter, kindly test your code on new db:

counter = fields.Integer('Customer Count', compute='count_test', store=True, default=False)

import random
def count_test(self):
    for record in self:
        record.counter = 1 + random.randint(1,9)
Related