This question has been flagged

I would like to automatically cancel the selection of a value in a many2one field when I click for example on the invisible radio button of the interface client: 


model.py file:

........

Product_id = fields.Many2one(‘product.template’)

State = fields.Selection((( ‘H’, ‘Hide’), (‘S’, ‘show’)), string=“state”)

hide_product = fields.Boolean()

@api.onchange

def _cancel_selection(self):

      If ( self.state is ‘H’):

           # cancel selection automatically on product_id field     

           Hide_product = True

           Self.product_id = “ “

............

view.xml file:

..........

< field name=“State” on_change=“1”  />

< field name=“product_id” attrs=“{‘invisible’ : [ (‘hide_product’, ‘=‘, True) ] } “  />

......

Avatar
Discard

what means cancel the selection ? reset product_id field data or set product_id field readonly or Hide.

Author

Reset product_id field data

Best Answer

As I understand your question you want to empty your Many2one field. So you can use the next code:
########################
@api.onchange
def _cancel_selection(self):
    If ( self.state is ‘H’):
        self.Product_id = None
        self.Hide_product = True
########################

I hope this could be helpfull for you.

Avatar
Discard
Author

Hi!

Yes it was my problem. Thank you it's working

Best Answer

Hi,

Better to explain your requirement here. I think you don't have an clear idea about what your are going to do with above code.

Avatar
Discard
Author

When we use many2one field, we can select the record in the database. So I would like to know how to reset many2one field ( no record selected)

Best Answer

Hello @Daquin sylar,

You can use this type of method to remove value from product_id based on selection field.


@api.onchange('state')

def onchange_state(self):

if self.state == 'H':

self.hide_product = True

self.product_id = False





Email: odoo@aktivsoftware.com

Skype: kalpeshmaheshwari

   

Avatar
Discard
Author

Hi !

I tried this method, but it isn’t working

Product_id is a many2one field not a Selection field

can i have your method's code?