跳至内容
菜单
此问题已终结

Hello Odoo Lovers
whenever I add an attachment in product.template model that time I need attachment in product.product model.

How can it's possible?

形象
丢弃
最佳答案

Hi Nikul,

Yes, you can do this by changing the domain in the JS file of the document module.

You would need to add the product.template object and template ID:

if (this.env.model == 'product.product') {
['res_model', 'in', [this.env.model, 'product.template']],
['res_id', 'in', activeId, templateId], # find the product templateID first
}

I hope this will help you to make progress.

形象
丢弃
编写者 最佳答案

Thank you Mr. Sudhir for answering and suggesting search_read method, But  Here I using Python code I added a product.template attachment in product.product model also same as vice versa.

_name = 'ir.attachment'
    @api.model
    def search_read(self, domain=None, fields=None, offset=0, limit=None, order=None):
        if domain and len(domain) == 2:
            res_id = False
            res_model = False
            for domain_list in domain:
                if domain_list[0] == 'res_id':
                    res_id = domain_list[2]
                elif domain_list[0] == 'res_model':
                    res_model = domain_list[2]
            if res_id and res_model and res_model == 'product.template':
                product_variant_ids = self.env['product.template'].browse(res_id).product_variant_ids.ids
                if product_variant_ids:
                    domain.insert(0, '|')
                    domain.insert(1, '&')
                    domain += ['&', ['res_id', 'in', product_variant_ids], ['res_model', '=', 'product.product']]
            elif res_id and res_model and res_model == 'product.product':
                product_rec = self.env['product.product'].browse(res_id)
                if product_rec:
                    domain.insert(0, '|')
                    domain.insert(1, '&')
                    domain += ['&', ['res_id', '=', product_rec.product_tmpl_id.id], ['res_model', '=', 'product.template']]
        return super(IrAttachment, self).search_read(fields=fields, offset=offset, limit=limit, domain=domain, order=order)

For Attachment Count in template & product Added a methods like this:

_name = "product.product"
    @api.multi
    def _compute_message_attachment_count(self):
        attachment_obj = self.env['ir.attachment']
        for record in self:
            domain = ['|', '&', ['res_id', '=', record.product_tmpl_id.id], ['res_model', '=', 'product.template'],
                       '&', ['res_id', '=', record.id], ['res_model', '=', 'product.product']]
            record.message_attachment_count = attachment_obj.search_count(domain)

_name = "product.template"
    @api.multi
    def _compute_message_attachment_count(self):
        attachment_obj = self.env['ir.attachment']
        for record in self:
            domain = ['|', '&', ['res_id', '=', record.id], ['res_model', '=', 'product.template'],
                       '&', ['res_id', '=', record.product_variant_ids.ids], ['res_model', '=', 'product.product']]
            record.message_attachment_count = attachment_obj.search_count(domain)

形象
丢弃
最佳答案

Hi, can I ask why do you need to attach it to product.product template? Is it to view on the website?

形象
丢弃
相关帖文 回复 查看 活动
2
8月 23
4572
0
10月 21
1855
1
9月 19
5406
1
6月 25
980
1
2月 25
1580