Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
2648 Vistas

Como evito que al cargar un archivo de excel para res.partners los duplique, ya anexe llave y sigue duplicando

Avatar
Descartar
Mejor respuesta

To avoid duplicating an Excel file for res.partners in Odoo/OpenERP when attaching a key, you can follow these steps:

1. Check for existing attachments: Before attaching the Excel file, verify if there are already existing attachments for the res.partner records. You can do this by searching for attachments related to the specific res.partner record.

2. Update existing attachment: If there is already an attachment for the res.partner record, you can update the existing attachment instead of creating a new one. To do this, you need to access the existing attachment record and update its content with the new Excel file.

3. Create a new attachment: If there are no existing attachments for the res.partner record, then you can proceed with creating a new attachment. Make sure to set the key or unique identifier to avoid duplication.

Here's an example of how you can achieve this in Odoo/OpenERP:

```python
# Assuming you have the res.partner record and the Excel file path

# Check for existing attachments
existing_attachment = self.env['ir.attachment'].search([
('res_model', '=', 'res.partner'),
('res_id', '=', partner_id),
('name', '=', 'Your Excel File Name.xlsx')
], limit=1)

if existing_attachment:
# Update existing attachment with the new file content
with open('path/to/new/excel/file.xlsx', 'rb') as file:
existing_attachment.write({
'datas': base64.b64encode(file.read()),
})
else:
# Create a new attachment
with open('path/to/new/excel/file.xlsx', 'rb') as file:
attachment = self.env['ir.attachment'].create({
'name': 'Your Excel File Name.xlsx',
'res_model': 'res.partner',
'res_id': partner_id,
'datas': base64.b64encode(file.read()),
})
```

Make sure to replace `'Your Excel File Name.xlsx'` with the actual name of your Excel file, and `'path/to/new/excel/file.xlsx'` with the appropriate file path.

By checking for existing attachments and updating them when necessary, you can avoid duplicating the Excel file for res.partner records in Odoo/OpenERP.

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
2
oct 23
1467
2
ago 24
1313
3
oct 24
2099
0
ago 24
967
2
oct 23
4675