0
votes

I want use a domain filter in my xml field like this :

domain="[('id','>=',field)]"

However, i don't understand how does it work !

Here my fields :

    responsible_partner = fields.Many2one(string="Responsible",
                                          comodel_name="res.partner",
                                          ondelete='restrict',
                                          domain="[('company_type','=','person')]",
                                          delegate=False)
    responsible_foyer = fields.Many2one(string="Foyer responsible", comodel_name="horanet.relation.foyer", copy=False)

Here my form view XML :

   <field name="responsible_partner" required="True" domain="[('id','=',responsible_foyer.partner_id)]" />
   <field name="responsible_foyer" />

Here my function :

@api.onchange('partner_id')
def _get_active_foyer(self):
    if self.partner_id:
        actually_partner_id = self.partner_id.name
        if actually_partner_id:
            records_foyer_id = self.env['horanet.relation.foyer'].search([('partner_id', '=', actually_partner_id)])

            for rec_foyer in records_foyer_id:
                if rec_foyer.foyer_id:
                    records_partner_id = self.env['horanet.relation.foyer'].search(
                        [('foyer_id', '=', rec_foyer.foyer_id.id)])
                    for rec_partner in records_partner_id:
                        if rec_partner.partner_id:
                            self.responsible_partner = rec_partner.partner_id.id
                            self.responsible_foyer = rec_partner.id
                            # self.test_foyer = rec_partner.foyer_id.id
                            print self.test_foyer 

Error :

Error: AttributeError: object has no attribute 'partner_id'

However, I have the field partner_id in my model : "horanet.relation.foyer".

Can you explain to me ?

Thanks

1

1 Answers

0
votes

You cannot access child records from XML (ie, the part after the dot). You can only refer to fields in the form itself, so you would have to make a new related field to get the value you want and then add it to your form as an invisible field, then you can use it as part of your domain.

Edit: detailed solution below:

You can add a related field in your python code like this:

responsible_foyer_partner_id = fields.Many2one('res.partner', related="responsible_foyer.partner_id", string="Responsible Foyer Partner")

Then in your XML you can add this field to the form view but keep in invisible. That will allow you to use it in your domain but not have it clutter up the form view.

<field name="responsible_foyer_partner_id" invisible="1"/>

Now you can use this field in your domain

<field name="responsible_partner" required="True" domain="[('id','=',responsible_foyer_partner_id)]" />