Guesmi
Guesmi

Reputation: 155

set default value for a many2one field

when the user set nom_rubrique that come the value of many2one filled will be set by default

 class A(models.Model):

    rubrique_id = fields.Many2one('risques.rubrique',
        string='Rubrique',
        default=_default_rubrique,
        index=True,
        track_visibility='onchange',
        change_default=True)

class B(models.Model):
    critere_ids = fields.One2many('risques.critere','rubrique_id',required=True, string="Critére d'évaluation")

Upvotes: 2

Views: 4572

Answers (4)

NM Naufaldo
NM Naufaldo

Reputation: 1160

The answer from Ridma Gimhani is almost correct. Since I can't edit his answer, I will edit his answer here

type = fields.Many2one('your.model',"String to display", 
                        default=lambda self: 
                        self.env['your.model']
                       .search([], limit=1))

We don't need to provide some search expression and the important thing we need to limit the result, if not, we will get an error

Upvotes: 0

Ridma Gimhani
Ridma Gimhani

Reputation: 133

use lambda for set default value:

type = fields.Many2one('your.model',"String to display", default=lambda self: self.env['your.model'].search([('name','=','value you need to set as default')]))

Its simple.

Upvotes: 0

Heroic
Heroic

Reputation: 980

It seems syntax error in your code, try below solution:

Also parent_id field does not exits in your reference risques.rubrique model

class critere(models.Model):
    _name = 'risques.critere'
    _rec_name = 'nom_critere'

    def _default_rubrique(self):
        #here you did mistake , your search method syntex was wrong
        #also it is better to use limit instead of [0] it may raise 
        #error if your search will return empty list
        return self.env['risques.rubrique'].search([('parent_id', '=', False)], limit=1).id

    nom_critere = fields.Char(string="Nom du Critere")
    rubrique_id = fields.Many2one('risques.rubrique',
                                  string='Rubrique',
                                  default=_default_rubrique,
                                  index=True,
                                  track_visibility='onchange',
                                  change_default=True)

class rubrique(models.Model):
    _name = 'risques.rubrique'
    _rec_name = 'nom_rubrique'
    nom_rubrique = fields.Char(string="Rubrique")
    critere_ids = fields.One2many('risques.critere', 'rubrique_id', required=True, string="Critére d'évaluation")

Upvotes: 1

Charif DZ
Charif DZ

Reputation: 14751

Than just in your context in the xml form or tree use this:

 <field field="id" invisible="1"/>
 <field name="critere_ids" context="{'default_rubrique_id': id}" />

but this works only if the record is created and you are trying to modify it. you will see that the m2o field field will have the same record that you are in.

but when you are in create mode the record is not created at that point so will never be able to pass it as default and the many2one will stay empty no metter what you do.

But even fi the user select another record when the user save the parent record you will see that the selected value has changed to the parent value. what i mean no metter what the user select on that m2o field the value will be ingnored and replaced by the parent id.

so best thing to is define a embaded tree and form for your one2many field:

<field name="critere_ids">
     <tree>
         <!-- list of field without rubrique_id field -->
     </tree>
     <form>
        <!-- new form structor  without rubrique_id field -->
     </form>
</field>

because in one2many field the user don't need to see that many2one at all he know that the record belong to the parent so see it, or select it at all.

Upvotes: 4

Related Questions