Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Filter using domain in a many2many relation view

By
Alessandro Pillan
on 7/11/13, 11:48 AM 12,060 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!

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!

evon_dun
on 10/7/13, 3:34 AM
1

René Schuster

--René Schuster--
1560
| 5 5 8
Weinheim, Germany
--René Schuster--

Challenge Everything!

René Schuster
On 7/17/14, 4:26 AM

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

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

 

Regards.

1
Gabriel
On 7/12/13, 7:47 AM

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>

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

Alessandro Pillan
on 7/12/13, 8:14 AM

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?

Gabriel
on 7/12/13, 8:52 AM

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

Alessandro Pillan
on 7/12/13, 10:07 AM

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.

Gabriel
on 9/10/13, 12:05 PM

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!

evon_dun
on 10/7/13, 3:34 AM
0

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 7/11/13, 11:48 AM
Seen: 12060 times
Last updated: 2/28/16, 9:14 AM