Siirry sisältöön
Menu
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Tämä kysymys on merkitty
3 Vastaukset
5864 Näkymät

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
Hylkää
Paras vastaus

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
Hylkää
Tekijä Paras vastaus

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
Hylkää
Paras vastaus

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

Avatar
Hylkää
Aiheeseen liittyviä artikkeleita Vastaukset Näkymät Toimenpide
2
elok. 23
4578
0
lokak. 21
1860
1
syysk. 19
5409
1
kesäk. 25
983
1
helmik. 25
1584