How to properly inherit m2o, o2m fields as well as their views and apply domains

Asked by Martin Collins

Inherited views are not displayed in many2one or one2many widgets. Instead they construct a default view. Also, domains have no effect. Consequently useability is compromised.

I attach a module which has two models: inheritance_bug_category inherits from res.partner.category.
  If you run this you will see the tree view has columns 'Complete Name' 'Child Categories' and 'Some Number'. If you switch to the form view you will see that the one2many widget is only showing one column 'Category Name'. Search on 'Parent Category' and you see the same view. Both these views should be the same as the tree view with three columns.

The second model is inheritance_bug_demo. Run this and search on 'Bug Category'. Once again you see the single column default view, not the three column view as specified in the xml.

The category_view specifies a domain of [('child_ids','=',[])] in order to show only the leaves of the category tree but this has no effect. It does work if you put the domain in the action, but the action is not invoked in the o2m or m2o widgets.

You can override fields_view_get() to force the right view to be used, but it shouldn't be necessary.

Question information

Language:
English Edit question
Status:
Solved
For:
Odoo Server (MOVED TO GITHUB) Edit question
Assignee:
No assignee Edit question
Solved by:
Olivier Dony (Odoo)
Solved:
Last query:
Last reply:

This question was originally filed as bug #674277.

Revision history for this message
Martin Collins (mkc-steadfast) said :
#1
Revision history for this message
Best Olivier Dony (Odoo) (odo-openerp) said :
#2

Hello Martin,

I converted your bug report to a question because it looks like a simple misunderstanding of OpenERP's API (and probably indicates a lack of clear examples in the documentation)

OpenERP supports what you are trying to do, but I'm afraid you have several errors in your module:

- you have use the "inherit as copy" type of inheritance in your objects, because you have specified a different _name in your inherited class. As a result you have created a different object that merely copies its parent's fields and methods. If you want to modify the parent model instead, you should not specify a _name, or use the same as the parent.
- as a result of the previous point, your inherited view is not correct because it mixes 2 models: it tries to inherit a view from a different model... and therefore will be ignored by both models! You should either correct the inheritance at object level or define a stand-alone view
- as for domains on o2m/m2m, you probably want to define them on the o2m/m2m field itself, or at the object level (domain=[(...)] in the _columns def), but the way you do it in the <tree> view makes no sense and is incorrect. Keep in mind that <tree> views are used in many contexts, not just o2m/m2m. BTW the domain you're trying to apply is very strange and would likely break your ability to use more than 2 levels of hierarchy if you had written it correctly... you probably want to do this differently

Revision history for this message
Martin Collins (mkc-steadfast) said :
#3

I want the shape and behaviour of the parent but for a different purpose and with different data so I think I have the correct type of inheritance.

So "inherit as copy" does not inherit views. A key piece of info. Now I know that and re-read the inheritance page it does kind of imply that in a backwards way. Perhaps you could get the docs folk to explicitly state it for the new version.
I have removed the inheritance from the view and it now works as I would like. Thanks.

I'm sure the first place I tried to put the domain was on the m2o field itself. I guess the bad view stopped it working.
You say my domain is strange. I've tested it with up to five levels and it works fine. If there's a more correct way I'd be happy to learn it.

Thanks again for setting me straight.

Revision history for this message
Martin Collins (mkc-steadfast) said :
#4

Thanks Olivier Dony (OpenERP), that solved my question.

Revision history for this message
Martin Collins (mkc-steadfast) said :
#5

Sure enough, the domain [('child_ids','=',[])] has stopped working in 6.0rc2.
For objects with left/right parents [('parent_right-parent_left','=',1)] works and for those without [('parent_id','!=',None)] works provided there are only two levels.
I'd still welcome suggestions for a robust, general and correct domain for selecting only leaf nodes of a tree or graph in a one2many field.

Revision history for this message
Martin Collins (mkc-steadfast) said :
#6

Sigh...
[('parent_right-parent_left','=',1)] doesn't work on a new database. The query in the log works in PGAdmin but not in OpenERP. I guess Olivier would think it very strange anyway.
So I read the relevant code in osv/expression.py and came up with [('child_ids','=',False)]. This works in new and old databases.