This question has been flagged
3 Replies
7264 Views

I try to add the sum of attachments which are added to a form - to show in the tree view. But I m not able to find the right synthax. Odoo 8.

Tried a lot of variations like

<field name="attachement_ids"/>

but did not hit the right one. Anybody knows? Thanks.

 

EDIT:

With the code given by Emipro Technologies in odoo 8 I get this traceball:

File "/opt/odoo/openerp/api.py", line 395, in new_api result = [method(rec, *args, **kwargs) for rec in self]

File "/media/cenguru-server/odoo-addons-test/geraete/models.py", line 114, in count_attachments attachment_ids = obj_attachment.search([('res_model','=','geraetespezifikation'),('res_id','=',record.id)]).ids

File "/opt/odoo/openerp/api.py", line 237, in wrapper return old_api(self, *args, **kwargs) TypeError: search() takes at least 4 arguments (2 given)

Which arguments are missing - where can I find documentation of functions like obj_attachment.search?

Could you help me again?

Avatar
Discard
Author Best Answer

I read about the used functions and changed the code to. 

@api.one

def count_attachments(self):

obj_attachment = self.env['ir.attachment']

for record in self:

record.attachment_count = obj_attachment.search_count([('res_model','=','geraet.geraetespezifikation'),('res_id','=',record.id)])

Should be faster. In my case its working.

Avatar
Discard

Yes, this is better, additionaly you remove linie record.attachment_count = 0

Best Answer

Try this:

    @api.one
    def count_attachments(self):
        obj_attachment = self.env['ir.attachment']
        for record in self:
            record.attachment_count = 0
            attachment_ids = obj_attachment.search([('res_model','=','account.invoice'),('res_id','=',record.id)])
            if attachment_ids:
                record.attachment_count = len(attachment_ids)

 

Avatar
Discard
Author

works perfect. Thanks.

Best Answer

You can have a fields.function


@api.one
def count_attachments(self):
    obj_attachment = self.pool.get('ir.attachment')
    for record in self:
        record.attachment_count =0
        attachment_ids = obj_attachment.search([('res_model','=','<<your model name>>'),('res_id','=',record.id)]).ids
        if attachment_ids:
            record.attachment_count =len(attachment_ids)


attachment_count = fields.Integer(string="Attachment Count" ,compute="count_attachments")

Hope this helps.

Avatar
Discard
Author

Thank you very much. Your code nearly worked for me. But I am to much of a user to debug the traceball myself. File "/opt/odoo/openerp/api.py", line 395, in new_api result = [method(rec, *args, **kwargs) for rec in self] File "/media/cenguru-server/odoo-addons-test/geraete/models.py", line 114, in count_attachments attachment_ids = obj_attachment.search([('res_model','=','geraetespezifikation'),('res_id','=',record.id)]).ids File "/opt/odoo/openerp/api.py", line 237, in wrapper return old_api(self, *args, **kwargs) TypeError: search() takes at least 4 arguments (2 given) Could you help me out, please?

Ok there may be a mistake, but the person that gave you the ans has copied my ans, may be he rectified the issue I am not sure. Anyways, it is more important to solve the issue. Its good you got the solution