Files
odoo_source/addons/product/models/ir_attachment.py
T
Victor Feyens 6af88b3b6a [FIX] product: documents creation & deletion
1) fix automatic creation of documents when attachments are added
to product templates/variants chatters

2) fix duplication of documents

3) fix deletion of documents

Delete child record before deleting parent record.
Before this commit, when trying to delete a product document,
we deleted the parent record first, before trying to delete
the child, which had been deleted (cascade) already.

Part-of: odoo/odoo#135894
2023-09-21 18:27:47 +00:00

26 lines
957 B
Python

# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, models
class IrAttachment(models.Model):
_inherit = 'ir.attachment'
@api.model_create_multi
def create(self, vals_list):
"""Create product.document for attachments added in products chatters"""
attachments = super().create(vals_list)
if not self.env.context.get('disable_product_documents_creation'):
product_attachments = attachments.filtered(
lambda attachment:
attachment.res_model in ('product.product', 'product.template')
and not attachment.res_field
)
if product_attachments:
self.env['product.document'].sudo().create(
{
'ir_attachment_id': attachment.id
} for attachment in product_attachments
)
return attachments