This question has been flagged
9 Replies
46897 Views

Hi all, I'm trying to understand how domain works in tree view for a many2many relationship in OpenERP 7.0. This is a dummy class Places:

class place_type(Model):
    _name = "place.type"
    _description = "Place Type"
    _columns = {
        'name': fields.char('Name', size=16, required=True),
    }
    _order = 'name asc'

place_type()

class place(Model):
    _name = "place"
    _description = "Place"
    _columns = {
        'name': fields.char('Name', size=64, required=True),
        'type': fields.many2one('place.type', string='Type'),
    }
    _order = 'name asc'

place_type()

class res_partner(Model):
    _inherit = 'res.partner'
    _columns = {
        'place_ids': fields.many2many('place', string="Dummy Places"),
    }

res_partner()

I'm just adding to partners some places defined by a name and a type, this is my view:

<!-- places page addition to partner form -->
<record model="ir.ui.view" id="view_partner_form_with_dummy_places">
        <field name="name">res.partner.form</field>
        <field name="model">res.partner</field>
        <field name="inherit_id" ref="base.view_partner_form"/>
        <field name="arch" type="xml">
            <xpath expr="(sheet/notebook/page)[last()]" position="after">
                <page string="Dummy Places">
                    <field name="place_ids" domain="('type', '=', 4)"/>
                </page>
            </xpath>
        </field>
</record>

I've tried in many ways to make domain working, in this case "4" is a real type id.
I also tried to insert obviously wrong domain statements, without getting any feedback error neither in server log nor in javascript console.
Is it completly ignored?
Latter, what I want to achieve is dynamically filtering partner places by a select field , is it possible?
I really appreciate any help!

Avatar
Discard

I want to use partner's name from 'partner_ids' field which is a 'many2many' field in the 'partner.ledger.webkit' model, in the attachment prefix for the report 'Partner Ledger Report' example 'object.company_id.name.pdf'. Please help!

Best Answer

The domain is a list of tuples, so use a list:

<field name="place_ids" domain="[('type', '=', 4)]"/>

 

Regards.

Avatar
Discard
Best Answer



Avatar
Discard
Best Answer

Hi Allessandro,

Update:

Domain is not a valid attribute for the field object in xml view.

It is however possible to define a functional field to filter a x2many field. Further information can be found in the nice Andreas Brueckl answer.


Have you tried: domain="('type.id', '=', 4)" instead of: domain="('type', '=', 4)" ?

For the dynamic filtering, you may try something like:

class res_partner(Model):
    _inherit = 'res.partner'
    _columns = {
        'type_id': fields.many2one('place.type', string='Type'),
        'place_ids': fields.many2many('place', string="Dummy Places"),
    }
res_partner()

And in the XML view:

<page string="Dummy Places">
   <field name="type_id" />
   <field name="place_ids" domain="('type.id', '=', type_id)"/>
</page>
Avatar
Discard
Author

Domain is still not working, now it doesn't show any record. But when I try to add a new place now it throw an error, it may be helpful:

File "/home/openerp/server/openerp/osv/expression.py", line 582, in check_leaf raise ValueError("Invalid leaf %s" % str(self.leaf)) ValueError: Invalid leaf type.id

Can we have more details on what you are intending to do. Would you like to hide some records of the existing many2many links or filter the Add tree view displayed when adding a new record?

Author

Yes, sorry, I would like to hide records where type is not equal to something. Next step field type_id added on your view should be a select

It seems that domain is not working when it is defined in XML view. I tried as well by adding a filter through a search view, and unfortunately it does not seems to work neither.

Finally what worked for a similar case where I had to display two times the same one2many field on the same view with different filters was to create two functional fields returning the filtered ids.

In your case one functional field returning the ids of place_ids that have the specified type_id would probably make it.

I want to use partner's name from 'partner_ids' field which is a 'many2many' field in the 'partner.ledger.webkit' model, in the attachment prefix for the report 'Partner Ledger Report' example 'object.company_id.name.pdf'. Please help!