Distinct records in Many2one()

Asked by mm alam

HI every one,

Scenario:

Object1's Data :

1 hi 2002 234234234
2 hi 2002 sdasdfasdf
3 hi 2003 asertdfhdfg

In another object form, I want to select the distinct records of Object1 using many2one() field.

Is it possible to write an extensive query for the many2one() field so that, only distinct records are shown to the user, while selecting from the Object1?

Currently, i can see the domain attribute, through which i can only implement some equality checks only.

Please advise.

Question information

Language:
English Edit question
Status:
Answered
For:
Odoo Server (MOVED TO GITHUB) Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Borja López Soilán (NeoPolus) (borjals) said :
#1

Why don't you use a selection widget with custom 'options'?

Take a look at the title of the partners: It is a selection built dynamically from the results of a query (res_partner_title elements with domain=partner):

def _partner_title_get(self, cr, uid, context={}):
    obj = self.pool.get('res.partner.title')
    ids = obj.search(cr, uid, [('domain', '=', 'partner')])
    res = obj.read(cr, uid, ids, ['shortcut','name'], context)
    return [(r['shortcut'], r['name']) for r in res] + [('','')]
    ...
class res_partner(osv.osv):
    ...
    _name = "res.partner"
    ...

    _columns = {
        ...
        'title': fields.selection(_partner_title_get, 'Title', size=32),
        ...

You could do something like that and just remove the duplicates on the filter.

Revision history for this message
xrg (xrg) said :
#2

On Monday 19 July 2010, you wrote:
> New question #118214 on OpenObject Server:
> https://answers.launchpad.net/openobject-server/+question/118214
>
> HI every one,
>
> Scenario:
>
> Object1's Data :
>
> 1 hi 2002 234234234
> 2 hi 2002 sdasdfasdf
> 3 hi 2003 asertdfhdfg
>
> In another object form, I want to select the distinct records of Object1
> using many2one() field.
>
>
>
> Is it possible to write an extensive query for the many2one() field so
> that, only distinct records are shown to the user, while selecting from
> the Object1?
>

I think /not/.

The reason is that even though you might see column-2 (say, the one that
contains same values) of your Object1, the many2one field will always pick the
records by their unique id (primary key). So they can't possible be distinct
on column-2.

It is more than a feature of the system. If you wanted to have some groupping,
you should have put that column in a separate table, where entries cannot be
duplicated. Then, reference that table in your Object1.

Revision history for this message
mm alam (masoom-alam) said :
#3

Hi,

You mean, we should first create a new table, put the distinct records in to it, and then select them from there? Don't you think so, it is a big problem, also, can lead to some integrity problems too later on.

Can we do this thing in OpenERP currently or not?

Revision history for this message
Borja López Soilán (NeoPolus) (borjals) said :
#4

"create a new table, put the distinct records in to it, and then select them from there"
You can also use a view for that, so you avoid any integrity problems ;)

Revision history for this message
mm alam (masoom-alam) said :
#5

Yeah, I see your point, create a view in the __init__.

However, is there is any work in Version 6 on this?

Also, can we solve this using the built-in "search" method some how?

Revision history for this message
xrg (xrg) said :
#6

On Tuesday 20 July 2010, you wrote:
> Question #118214 on OpenObject Server changed:
> https://answers.launchpad.net/openobject-server/+question/118214
>
> mm alam gave more information on the question:
> Yeah, I see your point, create a view in the __init__.
I think the views is entirely a different thing.. Cannot understand where they
could help, providing that you build a custom search query anyway.

> However, is there is any work in Version 6 on this?
> Also, can we solve this using the built-in "search" method some how?
>

Please, can we go back to the original question? See the title of this thread,
it talks about distinct *records*. Well, they are always distinct, because the
ID of the record makes it unique. That's what I said in the first place.
But, are you talking about distinct *values* for a group of records? Then,
that would be a different result set for the functions (python or any lang)
that we call. So, it means to have different functions than read(), search()
and/or browse().

Making any code that would return distinct /values/ for a group of records
with the current functions, would severely break the existing ORM API. We may
need a new set of search/read/browse functions.

Can you help with this problem?

Provide an answer of your own, or ask mm alam for more information if necessary.

To post a message you must log in.