0
votes

How populate select option on change other field. For example:

Default value for select option is store in database tbl_car (Audi, Opel, Mercedes, VW, Bmw). In other table tbl_car_user I store car_name and user_name ('Peter','Audi'). Now I want after change user_id (Select user Peter) in car select option get all car not include Audi (User Peter already use Audi).

Maybe like this:

for car in self.env['tbl.car'].search([]):
    for car_user in self.env['car.user'].search([('user_id','=','self.user_id.id]):
    if (car.name = car_user.name):
       print("DUPLICATE")
    else:
       print("ADD TO SELECT OPTION")

Any simple solution?

2

2 Answers

1
votes

my first answar is correct now i will give a solution if you don't want to change the selection:

Create a wizard to affect a car to user :

class AffectCar(model.TransientModel):
    _name = 'affect.user.car.wizard'
    use_id = fields.Many2one(..) # you know how you do it
    car_name = fields.Selection(selection='_get_car_selection', 'Car name')

    def _get_car_selection(self):
     """
       generate a selection for field car_name according to
       the default user_id passed to this form
     """
     # get all car name that this user don't have 
     # generate the selection [('car_name','car_name')..]
     return computed_selection

   def create_user_car(self):
    """ save a new tbbl_car_user record  """
    # this method is called from the form of the wizard
    # save the user_id and the car_name in tbl_car_user 

now add a button to the user form and call a method to open the wizard form with user_id by default is the same user

@api.multi()
def add_car(self):
    """
      open the wizard to add a car
      to this user 
    """
    return {
        'type': 'ir.actions.act_window',
        'view_mode': 'form',
        'view_type': 'form',
        'res_model':'affect.user.car.wizard',
        'target': 'new', 
        'context': {
             # pass the id the the user to the wizard
            'default_use_id': self.id,
        },
        }

one thing to prevent the user of you application from changing the user_id when the popup is shown make the user is in the form view of the wizard invisble="1"

<record id="add_car_wizard" model="ir.ui.view">
    <field name="name">tax.adjustments.wizard.form</field>
    <field name="model">tax.adjustments.wizard</field>
    <field name="arch" type="xml">
    <form>
        <group>
            <field name="user_id" invisible="1"/>
            <field name="car_name"/>
        </group>
        <footer>
            <button name="create_user_car" string="Add car" type="object" class="oe_highlight"/>
            or 
            <button string="Cancel" special="cancel" />
        </footer>
    </form>
    </field>
</record>
0
votes

This kind of problem don't use Selection, even when you find this, if you edit the record next time the selection will not know the value that it contain because odoo will fill the selection by all value except the value that it have. you will see uknown value on the selection field.

but if you want to do this don't use selection use many2one change the selection of car name to a Model (table in database) and use domain for you many2one field.

you cannot do this logic by selection this logic can be don with selection only for wizard.

field_selection = fields.Selection(selection='generate_selection')

def generate_selection(self):
    # compute selection
    return computed_selection

but this works when the view is load the first time now the value of the selection cannot be edited or changed with onchange event.