3

I am using the following code to add a new column in stock.picking.in object and update the no of attachments to it (to show in tree view the count of attachments).

class stock_picking(osv.osv):
_inherit = "stock.picking.in"
_name = 'stock.picking.in'
def count_attachments(self, cr, uid, ids, fields, arg, context):
    obj_attachment = self.pool.get('ir.attachment')
    for record in self:
        _logger.info("record now in tree view:"+record)
        record.attachment_count =0
        attachment_ids = obj_attachment.search([('res_model','=','stock.picking.in'),('res_id','=',record.id)]).ids
        if attachment_ids:
            record.attachment_count =len(attachment_ids)
    return record

_columns = {
            'attachment_count' :fields.function(count_attachments,method=True,string="Attachment Count" ,type='integer')
    }

stock_picking()

Then I have added the following line to the tree view.

<field name="attachment_count">

to show the count in tree view.

However the values are not getting updated and the count_attachments is not getting called.

Any help is appreciated. Thanks in advance!

Kannan_SJD
  • 1,022
  • 2
  • 13
  • 32

1 Answers1

1

Try following,

class stock_picking(osv.osv):
    _inherit = "stock.picking.in"
    _name = 'stock.picking.in'

    def count_attachments(self, cr, uid, ids, fields, arg, context=None):
        obj_attachment = self.pool.get('ir.attachment')
        res = {}
        for record in self:
            res[record.id] = 0
            _logger.info("record now in tree view:"+record)
            attachment_ids = obj_attachment.search([('res_model','=','stock.picking.in'),('res_id','=',record.id)]).ids
            if attachment_ids:
                res[record.id] = len(attachment_ids)
        return res

    _columns = {
                'attachment_count' :fields.function(count_attachments,method=True,string="Attachment Count" ,type='integer')
        }
  • Thanks empiro Tech.. Let me try this.! Just to know, is there any other way to do this like a one2Many field or so.? – Kannan_SJD Apr 16 '15 at 09:54
  • It's proper way to perform this kind of action. – Emipro Technologies Pvt. Ltd. Apr 16 '15 at 09:58
  • It is not working for me friend.! Can you say where should I add this.? I am adding this to a addon I have designed and inherited the "stock.picking.in" object. Is this correct? – Kannan_SJD Apr 16 '15 at 12:22
  • you have added it to the correct location, but when you run first time its calculating fields.function if you get any error it doesn't calculating another time, so you just need to do one thing comment this field in _columns and restart server then update module and again make it uncomment and then restart server and update module. it will done surely. – Emipro Technologies Pvt. Ltd. Apr 17 '15 at 04:21
  • For confirmation you should try with new record not with existing one, create new record and check that whether it works or not. – Emipro Technologies Pvt. Ltd. Apr 17 '15 at 04:48
  • This works in account.invoice object, however when it comes to stock.picking object ( Reciept of goods) in inventory this does not work. Need help.! – Kannan_SJD Apr 20 '15 at 10:51