Odoo Help

1

Show number of attachments in tree view v8 [solved]

By
Tobias Frankl
on 1/23/15, 1:19 PM 2,427 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?

2
Tobias Frankl
On 1/24/15, 4:21 AM

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.

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

Zbik
on 1/24/15, 4:31 AM
2
Zbik
On 1/23/15, 4:40 PM

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)

 

works perfect. Thanks.

Tobias Frankl
on 1/23/15, 4:53 PM
2

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.

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?

Tobias Frankl
on 1/23/15, 4:11 PM

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

Emipro Technologies Pvt. Ltd.
on 1/23/15, 11:51 PM

About This Community

This platform 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.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)

Stats

Asked: 1/23/15, 1:19 PM
Seen: 2427 times
Last updated: 12/10/16, 2:48 AM