[FIX] website_form: prevent crash on form many2one attachment upload

The current implementation of the many2one file upload in website form
will lead to a traceback in case of m2o fields.
It is currently only working with x2many fields. Note that it was
introduced as such with [1].

Step to reproduce:
- install website_sale
- drag & drop form snippet and click on it
- select "create customer" as action option
- add new existing field
- select "Main attachment" and save
- try to submit the form with a file uploaded in that new field
-> Traceback `ValueError: Wrong value for..`

This commit makes it work for all type of relational field.

[1]: https://github.com/odoo/odoo/commit/a77f5cf42faa75a2dd3931d83ad8ea86648248c0

closes odoo/odoo#104241

X-original-commit: addea33a8cb6c47bacdbfad0f3082f88763b59ef
Signed-off-by: Romain Derie (rde) <rde@odoo.com>
This commit is contained in:
Wolfgang Taferner
2022-10-27 11:38:50 +02:00
parent bcf2cf7d65
commit 7f684fcac0
+5 -1
View File
@@ -261,7 +261,11 @@ class WebsiteForm(http.Controller):
}
attachment_id = request.env['ir.attachment'].sudo().create(attachment_value)
if attachment_id and not custom_field:
record.sudo()[file.field_name] = [(4, attachment_id.id)]
record_sudo = record.sudo()
value = [(4, attachment_id.id)]
if record_sudo._fields[file.field_name].type == 'many2one':
value = attachment_id.id
record_sudo[file.field_name] = value
else:
orphan_attachment_ids.append(attachment_id.id)