Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
1 Beantwoorden
2014 Weergaven

After saving the child record with the parent ID as a reference, the parent ID should not appear in the Many2One field.

I have written the following code but it is not working. it is working in odoo 15 but not odoo 17


class CustomPreset(models.Model):

    _name = 'custom.preset'

    _inherit = ['mail.thread', 'mail.activity.mixin']


    name = fields.Many2one('custompreset.master', string='Name')

    rel_id = fields.One2many('custompreset.child', 'parent_field_id', string='Detail')

    search_dp = fields.Char(string='Search')



@api.onchange('search_dp')

    def onchange_test_domain_fiedl(self):

        mini_rec = self.env['custom.preset'].search([]).name

        available_ids = []

        for i in mini_rec:

            available_ids.append(i.id)

          return {'domain': {'name': [('id', '!=', available_ids)]}}



Avatar
Annuleer
Beste antwoord

Hello Yoganandam Gopal,

One possible issue could be with the "onchange" method. Make sure you don’t have two methods with the same name.


Also, it’s important to ensure that you have added the necessary dependencies to your "__manifest__.py".

Here’s a revised version of your code:

class CustomPreset(models.Model):

    _name = 'custom.preset'

    _inherit = ['mail.thread', 'mail.activity.mixin']


    name = fields.Many2one('custompreset.master', string='Name')

    rel_id = fields.One2many('custompreset.child', 'parent_field_id', string='Detail')

    search_dp = fields.Char(string='Search')


    @api.onchange('search_dp')

    def onchange_test_domain_field(self):

        mini_rec = self.env['custom.preset'].search([('id', '!=', self.id)]).name

        available_ids = []

        for i in mini_rec:

            available_ids.append(i.id)

        return {'domain': {'name': [('id', 'not in', available_ids)]}}


In this revised code, I’ve changed the search domain in the "onchange" method to exclude the current record’s ID. This should prevent the current record from appearing in the "Many2one" field dropdown.


Hope this helps

Regards,
Maciej Burzymowski

Avatar
Annuleer
Gerelateerde posts Antwoorden Weergaven Activiteit
0
mrt. 15
5097
1
jul. 24
2997
0
feb. 24
14
3
dec. 23
1813
4
mei 25
2521