This question has been flagged
2 Replies
4269 Views

- When clicking on the button displays the list of all documents uploaded to that record

- Documents are taken from ir.attachment model, there are 2 logic:

1 / resource model = res.partner

2 / res_id = res_id of record

- Create a compute field for the button to count all uploaded documents.

my code:

class ResPartner(models.Model):
_inherit = "res.partner"

x_phanloai = fields.Selection(string="Phân loi",
selection=[('leader', 'Leader'), ('end-user', 'End-user')])
x_masothue = fields.Char(string='Mã s thuế', required=True)
x_mien = fields.Char(string='Min', related='state_id.x_mien', readonly=True)
x_attachment_kh = fields.Many2many('ir.attachment', string='Attachment')
attachment_count = fields.Integer(compute='_compute_attachment_count', string='Attachment')

ếm s tài liu khi được upload
@api.depends('x_attachment_kh')
def _compute_attachment_count(self):
for record in self:
total = 0
if record.x_attachment_kh:
for i in record.x_attachment_kh:
total += 1
record.attachment_count = total
---------
<button name="toggle_active" position="before">
<button class="oe_stat_button" type="action" name="%(base.action_attachment)d"
icon="fa-file-text-o"
context="{'search_default_res_id': id}">
<field string="Hp Đng" name="attachment_count" widget="statinfo"/>
</button>
</button>
All help is helpful for the bowl

Avatar
Discard
Best Answer

Hi, 

@api.multi
def _compute_attachment_count(self):
        Attachment = self.env['ir.attachment']
        for record in self:
            record.attachment_count = Attachment.search_count([('res_model', '=', self._name), ('res_id', '=', record.id)])

Upvote if this helps.

Thanks.


Avatar
Discard
Author Best Answer

thank you ibrahim, it does not work properly. I don't understand why it happened like that.

The problem becomes more complex than I think. when create does not manually res_model and res_id, so it cannot be counted.

Avatar
Discard

does my answer work properly for you ? or is there a problem with it ?

Author

nó không hoạt động trong khi create or import. tôi không biết nó ra lỗi gì mà không oạt động