Skip to Content
Menu
This question has been flagged
3 Replies
366 Views

Hello, I'm trying to add new fields to the partner_autocomplete_many2one widget, how to do it?


Thanks in advance.

Avatar
Discard
Best Answer

1. Override the autocomplete controller in Python:
from odoo.addons.base.controllers.partner_autocomplete import PartnerAutocomplete

from odoo.http import route, request


class PartnerAutocompleteExtended(PartnerAutocomplete):


    @route('/partner_autocomplete', type='json', auth="user")

    def partner_autocomplete(self, **kwargs):

        result = super().partner_autocomplete(**kwargs)

        for partner in result.get('partners', []):

            rec = request.env['res.partner'].sudo().browse(partner['id'])

            partner['x_custom_field'] = rec.x_custom_field

        return result

2. Extend the JS widget to use the custom field:

odoo.define('your_module.PartnerAutocompleteExtended', function (require) {

    "use strict";

    const FieldPartner = require('base.PartnerAutocompletion');

    FieldPartner.include({

        _parseResponseData(partner) {

            const result = this._super(...arguments);

            result.x_custom_field = partner.x_custom_field;

            return result;

        }

    });

});

3. Load your JS in assets:

<template id="assets_backend" inherit_id="web.assets_backend">

    <xpath expr="." position="inside">

        <script type="text/javascript" src="/your_module/static/src/js/custom_partner_autocomplete.js"/>

    </xpath>

</template>


Avatar
Discard
Best Answer

Hello @Odd Dellarobbia,


Just a quick clarification:

The widget partner_autocomplete_many2one is not part of the standard Odoo framework.

Instead, Odoo uses widgets like field_partner_autocomplete or res_partner_many2one for partner autocomplete functionality, including search and selection, especially on website forms.

For Example:

Python Side :-

//Code_1 in Comment//


XML View Inheritance:

//Code_2 in comment//


If you have any questions or need further clarification, feel free to reach out.


Thanks & Regards,

Email:  odoo@aktivsoftware.com           

Skype: kalpeshmaheshwari 

Avatar
Discard

Code_1 :

# -*- coding: utf-8 -*-
from odoo import fields, models

class ProjectTask(models.Model):
_inherit = 'project.task'

customer_contacts = fields.Many2one('res.partner',
string='Customer Contacts'
)

Code_2 :

<record id="view_task_form_inherit_custom_fields" model="ir.ui.view">
<field name="name">project.task.form.inherit.custom</field>
<field name="model">project.task</field>
<field name="inherit_id" ref="project.view_task_form2"/>
<field name="arch" type="xml">
<field name="partner_id" position="after">
<field name="customer_contacts" widget="res_partner_many2one"/>
</field>
</field>
</record>