Zum Inhalt springen
Menü
Sie müssen registriert sein, um mit der Community zu interagieren.
Diese Frage wurde gekennzeichnet
3 Antworten
5852 Ansichten

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
Verwerfen
Beste Antwort

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
Verwerfen
Autor Beste Antwort

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
Verwerfen
Beste Antwort

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

Avatar
Verwerfen
Verknüpfte Beiträge Antworten Ansichten Aktivität
2
Aug. 23
4573
0
Okt. 21
1856
1
Sept. 19
5407
1
Juni 25
980
1
Feb. 25
1581