2
votes

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")
4
rubrique_id will set automatically where you have used this field as reference field on your One2many field. - Hilar AK
What many2one field you are talking about rebrique_id, you mean that when user hit add item on the one2many field the many2one field is empty? - Charif DZ
@Tchi Odoo yes that it - Guesmi
I do this function def _default_rubrique(self): return self.env['risques.rubrique'].search([], limit=1, order='id desc') and in class rubrique I need to do function save autimatiqually the current record when user clic add item on the one2many but I d'ont know how - Guesmi

4 Answers

4
votes

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.

1
votes

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")
0
votes

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.

0
votes

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