Skip to Content
Menu
This question has been flagged
2 Replies
5661 Views

hello,
how can i filter my One2many field base on Many2one field?

exemple, i have 2 classes like:

--------begin-----------
class btp_caution(models.Model):
    _name = "btp.caution"
    _description = "Les cautions"

    name = fields.Char(string='Libellé caution', required=True)
    nemero = fields.Char(string='Numéro caution')
    banque_id = fields.Many2one('btp.banque', 'Banque')
    projet_id = fields.Many2one('btp.projet', 'Projet')
    etat_caution = fields.Many2one('btp.etatcaution', 'etat_caution_')

class btp_etatcaution(models.Model):
    _name = "btp.etatcaution"
    _description = "voir les etats des differentes cautions filtres par banque et par projet/marche"
    _auto = 'False'

    name = fields.Char('Name')
    banque_id = fields.Many2one('btp.banque', 'Banque')
    projet_id = fields.Many2one('btp.projet', 'Projet')
    ligne_caution_etat = fields.One2many('btp.caution', 'etat_caution', 'etat_caution')
--------end-----------

and i have view related to this last one (btp.etatcaution) like:

--------begin-----------
<record id="view_btp_etatcaution_form" model="ir.ui.view">
    <field name="name">etatcaution</field>
    <field name="model">btp.etatcaution</field>
    <field name="arch" type="xml">
        <form string="etatcaution" class="oe_form_configuration" create="false">
            <sheet>
                <group>
                    <field name="banque_id" class="oe_inline"/>
                    <field name="projet_id" class="oe_inline"/>
                </group>
            </sheet>
            <notebook>
                <page string="Etat caution">
                    <div class="row"><div class="col-xs-12 text-muted">
                    <field name="ligne_caution_etat" >

                        <tree editable="bottom" string="Etat caution" create="false">
                            <field name="name"/>
                            <field name="nemero"/>
                            <field name="banque_id"/>
                            <field name="projet_id"/>
                        </tree>
                </field>
                </div></div>
            </page>
        </notebook>
    </form>
    </field>
</record>
<record id="action_etatcaution" model="ir.actions.act_window">
    <field name="name">etatcaution</field>
    <field name="type">ir.actions.act_window</field>
    <field name="res_model">btp.etatcaution</field>
    <field name="view_type">form</field>
    <field name="view_mode">tree,form</field>
    <field name="res_id">1</field>
    <field name="view_id" ref="view_btp_etatcaution_form"/>
</record>
--------end-----------

how can i do to filter my notebook data in oder to return only record which banque_id and projet_id are choosed in the form view?
e.g : the value of banque_id and projet_id in all notebook lines should be the same that those we have shoosed in <sheet> .... </sheet>



Avatar
Discard

Read this documentation for odoo customization: https://learnopenerp.tumblr.com/

Best Answer

If you want to set default value of O2m field from the field of your parent object, you can pass default value in the context of O2m field.

Try following code:

<field name="ligne_caution_etat"
       context="{'default_banque_id': banque_id, 'default_projet_id': projet_id}"> 

Let me know if you are trying something else.

Avatar
Discard
Author

Nothing change on my view. Insn't it any way to use filtered() in the model side like:

class btp_etatcaution(models.Model):

...

ligne_caution_etat = fields.One2many('btp.caution', 'etat_caution', 'etat_caution').filtered(lambda r: r.banque_id == "etat_caution.banque_id")

Best Answer

Try this:

<field name="ligne_caution_etat" domain=[('banque_id', '=', parent.banque_id)]>

.....

</field>

Avatar
Discard
Author

mothing change on my view. insn't any way to use filter in the model side like:

class btp_etatcaution(models.Model):

...

ligne_caution_etat = fields.One2many('btp.caution', 'etat_caution', 'etat_caution').filtered(lambda r: r.banque_id == "etat_caution.banque_id")