Ir al contenido
Menú
Se marcó esta pregunta

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?

Avatar
Descartar
Mejor respuesta

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.

Avatar
Descartar
Autor Mejor respuesta

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)

Avatar
Descartar
Mejor respuesta

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

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
2
ago 23
4551
0
oct 21
1852
1
sept 19
5396
1
jun 25
975
1
feb 25
1572