What determines which More button an option shows in? - openerp-7

In OpenERP there is the More button in the tree view, and the More button in the form view. Sometimes options show in one, or the other, or both -- what determines which one an option shows in?

Which <More> button an option is available in is determined by the table:field ir.actions.act_window:multi and ir.values:key2 pair.
The <act_window> shortcut (which makes the option available only in the form view More) looks like this:
<act_window
name="My Custom Name Here"
id="model_table_whatever_name"
res_model="model.table"
src_model="another_model.table"
/>
which, by default, also sets:
multi = False
key2 = 'client_action_relate'
view_type = 'form'
view_mode = 'tree,form'
target = 'current'
The value combinations/results of multi/key2are:
multi / key2 --> tree More / form More
0 / client_action_relate --> No / Yes
1 / client_action_relate --> Yes / No
1 / client_action_multi --> Yes / No
0 / client_action_multi --> Yes / Yes
If you need/want more control over all the fields created in the two tables that the shortcut effects:
<record id="action_model_table_whatever_name" model="ir.actions.act_window">
<field name="name">My Custom Name Here</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">model.table</field>
<field name="src_model">another_model.table</field>
<field name="multi" eval="0"/>
... more fields here ...
</record>
<record id="model_table_whatever_name" model="ir.values">
<field name="name">My Custom Name Here</field>
<field name="model">sample.request</field>
<field name="value" eval="'ir.actions.act_window,' + str(ref('action_model_table_whatever_name'))"/>
<field name="key2">client_action_relate</field>
... more fields here ...
</record>
Note: While the default key2 value in the act_window shortcut is 'client_action_relate', the default value for key2 when using the record format is 'tree_but_open' -- so you can omit it when using the shortcut, but you must include it when using the record style.

Related

How to keep the order of the records added in a many2many field the way I wrote them?

I want to keep the order of the records in the many2many field, the way I added them using the write method, but Odoo use the alphanumerical order by default and overwrote the order I put them in. How can I keep it in the same order when I wrote them, is that possible ?
Here is the model code:
# -*- coding: utf-8 -*-
from odoo import models, fields, api
class projet_richat(models.Model):
_inherit = 'project.task'
competence_ids = fields.Many2many('competence.competence',string='Compétences')
consultants=fields.Many2many('hr.employee', string="Consultants")
def search_comp(self):
if len(self.competence_ids) != 0:
consults=self.env['hr.employee'].sudo().search([])
list=[]
for cmp in self.competence_ids:
print(cmp.competence)
for cons in consults:
print(cons.name)
for c in cons.competences:
if c.competence.competence==cmp.competence:
list.append((cons.id,c.competence.competence,c.niveau))
sorted_by_level = sorted(list, key=lambda tup: tup[2], reverse=True)
print(sorted_by_level)
for sorted_cons in sorted_by_level:
self.write({'consultants': [[4, sorted_cons[0]]]})
and here is the view code:
<odoo>
<data>
<record model="ir.ui.view" id="project_inherit_form">
<field name="name">project.inherit.form</field>
<field name="model">project.task</field>
<field name="type">form</field>
<field name="inherit_id" ref="project.view_task_form2"/>
<field name="arch" type="xml">
<field name="user_ids" position="before">
<field name="competence_ids" widget="many2many_tags"/>
</field>
<field name="parent_id" position="after">
<field name="consultants">
<tree>
<field name="name"/>
</tree>
</field>
</field>
<button name="action_assign_to_me" position="after">
<button name="search_comp" string="Test" class="btn-primary"
type="object"
/>
</button>
</field>
</record>
</data>
</odoo>
Thanks in advance.
The many2many field will use the list view to show its content, the default order is defined by the _order attribute on the related model.
You can alter the default ordering by setting the default_order parameter on the tree tag
From the List view documentation:
default_orderoverrides the ordering of the view, replacing the model’s order (_order model attribute). The value is a comma-separated list of fields, postfixed by desc to sort in reverse order:
<tree default_order="sequence,name desc">
To keep the order you want, you can add an integer field sequence and use it to order records (_order='sequence') then in search_comp function set the sequence field value for each line

Odoo Hide Many2one Field when Empty

I have a Many2one field that I want to hide when there is no value. I try from this solution, but it didn't work and give me an error when trying to upgrade.
<field name="parent_id" attrs="{'invisible': [('parent_id', '!=', False)]}"/>
I had this problem too and I solved this with define a dummy field in my model and use this in view like this:
'''
hide = fields.Boolean(string="Hide", compute="_set_hide", store=False)
#api.depends('parent_id')
def _set_hide(self):
if self.parent_id.id:
self.hide = False
else:
self.hide = True
'''
and in xml you should write like this:
<field name="hide" invisible="1"/>
<field name="parent_id" attrs="{'invisible': [('hide', '='True)]}"/>
If you want the field to be hidden when it is empty, the code is as follows:
<field name="parent_id" attrs="{'invisible': [('parent_id', '=', False)]}"/>

Odoo : limit editable rows on editable tree

Suppose I have an editable tree
<tree editable="top">
<field name="date">
<field name="value">
</tree>
Now suppose I want to let the user edit the values for 3 most recent dates, but the others should remain readonly.
How would I do that ?
Well, you could add a boolen field to the model. which will be a computed field. based on that field you could apply the read-only attrs as following:
class TheModel(models.Model):
_name = 'The.Model'
old_dated = fields.Boolean(compute='_old_dated_rec')
date = fields.Date()
value = fields.Integer()
#api.model
def _old_dated_rec(self):
"""define the condition of old dated records which could be as"""
recent_rec = self.search([], order='date desc', limit=3)
old_rec = self.search([('id', 'not in', recent_rec._ids)])
old_rec.write({'old_dated': True})
Then you could apply a scheduler to run everyday calling such method
<field name="old_dated" invisible="1" />
<field name="date" attrs="{'readonly':[('the_boolen_field','=',True)]}"/>
in such way, the compute method will update the boolean field.

Display multiple one2many field with different domain dynamically?

I have a model(modelA) with one2many field related to another model(modelB) and one of the fields in modelB is a category field, which is a many2one field. The requirement is to have a one2many field displayed for each category. So if there are 2 categories named 'category1' and 'category2', the form view of modelA should have 2 one2many fields, one which displays records of having category1 and another for category2(which could possibly done using domain).
For eg modelA and modelB has the following structure.
class classA(models.Model):
_name = 'modelA'
modelA_one2manyfield = fields.One2many('modelB', 'modelB_many2onefield')
class classB(models.Model):
_name = 'modelB'
name = fields.Char()
category = fields.Many2one('modelC')
modelB_many2onefield = fields.Many2one('modelA')
How would i go about implementing a form view for modelA so that for each category(which can be added by the user, hence there can be of any number of categories) there is a seperate one2many field.
What you are asking take a lot of time to give a very good answer one of the way that i think you need to try is override the fields_view_get because this is the method that retreive the view and here you can change the arch field to add a costum field take a look at this tutorial :
Tutorial for dynamic view
but i think you will have a problem, because even when you put the domain on the one2many field in XML, odoo will not filter
the record when the loading happen on the view :
<!-- here all record are shown but the expected behavior is the one2many should be empty -->
<field name="one2many_field_name" readonly="1" nolabel="1" domain="[('id', '=', False)]">
but when i add this field to the python declaration
# here no record will be shown on the view and that's what was expected
one2many_field_name = fields.One2many(..., domain=[('id', '=', False)])
so the question adding one2many field to arch via fields_view_get is easy but the problem is filtring data !!
It's technically not possible. Because you can't have 2 times the same field in the same view.
But you can create a specific widget to showing what you want. How you can see in the timesheet view (My Current timesheet menu).
This is a little tutorial to created a widget.
https://www.odoo.com/documentation/10.0/howtos/web.html#widgets-basics
This not an answer but you can say a tutorial example of dynamic view :
modul structur:
->dynamic_view
--> __ini__.py
--> models.py
--> views.xml
--> __manifest__.py
__manifest__.py :
# -*- coding: utf-8 -*-
{
'name' : 'Dynamic view',
'version' : '1.0',
'summary': 'Tutorial for Dynamic view',
'sequence': 30,
'description': """
This Module is for showing that you can update the code of the view
when it's called and even create new field without having to use python
code at all
""",
'category': 'StackOverFlow',
'depends' : ['base_setup',],
'data': [
'views.xml'
],
'installable': True,
'application': True,
'auto_install': False,
}
__init__.py :
# -*- coding: utf-8 -*-
from . import models
models.py :
# -*- coding: utf-8 -*-
from odoo import models, fields, api
class Person(models.Model):
_name = "training.person"
name = fields.Char("Full name")
class Car(models.Model):
_name = "training.car"
name = fields.Char("Car name")
mark_id = fields.Many2one(comodel_name="training.mark", string="Mark")
owner_id = fields.Many2one(comodel_name="training.person", string="Owner")
person_view_id = "dynamic_view.dgapr_form_person"
# here default arch value body in the view contains only
# name field but as we create new mark we add others field
person_view_arch = """
<group>
<field name="name"/>
</group>
"""
class Mark(models.Model):
_name = "training.mark"
name = fields.Char("Mark")
#api.model
def create(self, values):
"""
when we create a category we add one2many field to person view
TODO: when we unlink a category we need to remove the one2many
name of field is : x_mark_{id of deleted record}
"""
rec_id = super(Mark, self).create(values)
o2m_field = {
# fields created using orm method must start with x_
"name": "x_mark_%s"% rec_id.id,
"field_description": "Mark %s" % rec_id.name,
"ttype": "one2many",
"relation": "training.car",
"relation_field": "owner_id",
"stored": True,
"domain": "[('mark_id','=', %s)]"%rec_id.id,
"model_id": self.env.ref("dynamic_view.model_training_person").id,
}
# add on2many field to ir.model.fields
self.env["ir.model.fields"].create(o2m_field)
self.update_arch()
return rec_id
def update_arch(self):
"""
when ever we create or delete a mark record
we need to update the the view to add new one2many field
if we want to hide the one2many field in view that don't have
any record we should create compute field to use attrs features
"""
view_id = self.env.ref(person_view_id)
o2m_fields_ids = self.env['ir.model.fields'].search(
[
('model_id', '=', self.env.ref("dynamic_view.model_training_person").id),
('ttype', 'like', 'one2many'),
('relation_field', 'like', 'owner_id')
])
o2many_arch = ""
for o2m_id in o2m_fields_ids:
o2many_arch = o2many_arch + """
<group col="1" string="%s">
<field name="%s" noloable="1" />
</group>
""" % (o2m_id.field_description, o2m_id.name,)
arch_begin = """
<form>
<sheet>
"""
arch_close = """
</sheet>
</form>
"""
arch_body = person_view_arch + o2many_arch
new_arch = arch_begin + arch_body + arch_close
# update the arch of the view in database
view_id.arch = new_arch
views.xml:
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<data>
<record id="dgapr_form_car" model="ir.ui.view">
<field name="name">car.form</field>
<field name="model">training.car</field>
<field name="arch" type="xml">
<form >
<sheet>
<group>
<field name="name"/>
<field name="mark_id"/>
<field name="owner_id"/>
</group>
</sheet>
</form>
</field>
</record>
<record id="dgapr_action_car" model="ir.actions.act_window">
<field name="name">Cars</field>
<field name="res_model">training.car</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem id="menu_root_training" name="Training"/>
<menuitem id="menu_ch_car" name="Cars" parent="menu_root_training" action="dgapr_action_car"/>
<record id="dgapr_form_person" model="ir.ui.view">
<field name="name">dgapr.form.person</field>
<field name="model">training.person</field>
<field name="arch" type="xml">
<form>
<sheet>
<group>
<field name="name"/>
</group>
</sheet>
</form>
</field>
</record>
</data>
</odoo>
i found out that you can create field using ORM method even compute field. i think creating a widget is better but good to know that wen can create costum fields .
Hope this helps you
Note i didn't create a menu for person record but you can see the view by clicking on the owner_id in the car form if the new one2many field not shown just refresh the page.

Populate other field with value

I have a test many2one field. When it is populated I want the partner_id field to use the partner associated with that field. Following is not working:
<field name="partner_id" required="1"/>
<field name="x_test" context="{'partner_id': parent.partner_id}" />
you should try this :
<field name="x_test" context="{'default_partner_id': partner_id}" />
I don't know what you mean by parent.partner_id this works if you have a field named parent in the same view.
i assume you wanna put same value of partner_id in x_test field, then use related field
partner_id = fields.Many2one('res.partner', string="partner")
x_test = fields.Many2one('res.partner',related='partner_id', string="X Test")
in XML
<field name="partner_id" required="1"/>
<field name="x_test" />