How to delete default empty select box option in Odoo 11?

I'm using many2one field in Odoo 11. My select box's first option coming empty as default but I don't want to this.

<select class="o_input o_field_widget o_required_modifier" name="department_id" id="o_field_input_28">
 <option value="false"></option>
 <option value="4">Test Departma</option>
</select>

If there is one option except default empty option in the select box then it will be selected initially. Otherwise, It can be selected empty option initially.

Please help me.

Upvotes: 1

Views: 1457

Answers (1)

CZoellner
CZoellner

Reputation: 14778

You could either set a default in the client or implement it in the code.

Code

@api.model
def default_department_id(self):
    # just an example! implement your own default
    # always return a recordset (even an empty one)!
    return self.env['my.department.model'].search([], limit=1)

department_id = fields.Many2one(
    comodel_name="my.department.model", default=default_department_id)

Client:

  1. Activate debug mode
  2. switch to form view of your model where default value should be set (create empty record)
  3. set your default value in the field
  4. open debug mode menu "Set Defaults" (see screenshot from odoo runbot)
  5. fill the assistant (PopUp) and click button "Save default"
  6. close empty or new created record (you don't need to save it)

enter image description here

Upvotes: 2

Related Questions