I have this action server:
def obtener_todos_los_adjuntos_en_un_zip(self):
tab_id = []
for invoice in self:
tab_id.append(invoice.id)
base_url = self.env['ir.config_parameter'].get_param('web.base.url')
url = f'{base_url}/web/binary/descargar_adjuntos?ids={"-".join(str(x) for x in tab_id)}'
return {
"type": "ir.actions.act_url",
"url": url,
"target": "new",
}
In my xml:
<odoo>
<record id="account_move_action_server"
model="ir.actions.server">
<field name="name">Descargar todos los adjuntos</field>
<field name="model_id"
ref="model_account_move" />
<field name="binding_model_id"
ref="model_account_move" />
<field name="state">code</field>
<field name="code">
if records:
records.obtener_todos_los_adjuntos_en_un_zip()
</field>
</record>
</odoo>
But it is not doing anything with the url passed to it. What do I need to do to make this action.url work?
So I need to return to a "action" var:
<odoo>
<record id="account_move_action_server"
model="ir.actions.server">
<field name="name">Descargar todos los adjuntos</field>
<field name="model_id"
ref="model_account_move" />
<field name="binding_model_id"
ref="model_account_move" />
<field name="state">code</field>
<field name="code">
if records:
action = records.obtener_todos_los_adjuntos_en_un_zip()
</field>
</record>
</odoo>
From odoo docs:
The code segment can define a variable called action, which will be >returned to the client as the next action to execute:
<record model="ir.actions.server" id="print_instance">
<field name="name">Res Partner Server Action</field>
<field name="model_id" ref="model_res_partner"/>
<field name="state">code</field>
<field name="code">
if record.some_condition():
action = {
"type": "ir.actions.act_window",
"view_mode": "form",
"res_model": record._name,
"res_id": record.id,
}
</field>
</record>
will ask the client to open a form for the record if it fulfills some condition
Related
I have a parent model (Plant) which sends the active_id (plant_id) in the context as a default:
class Plant(models.Model):
_name = 'db.plant'
_description = 'db.plant'
name = fields.Char("Name", required=True)
...
Plant view with a many2one relation between them:
<odoo>
<data>
<record id="plant_list_view" model="ir.ui.view">
<field name="name">db.plant.view</field>
<field name="model">db.plant</field>
<field name="arch" type="xml">
<tree>
<field name="name"/>
...
</tree>
</field>
</record>
<record id="plant_form_view" model="ir.ui.view">
<field name="name">db.form.view</field>
<field name="model">db.plant</field>
<field name="arch" type="xml">
<form>
<sheet>
<group string="Plant">
<field name="name"/>
...
</group>
<group string="Components">
<notebook>
<page string="Data Sources">
<field name="data_source_ids" context="{'default_plant_id': active_id}">
<tree >
<field name="name"/>
...
<!-- <field name="plant_id"/> -->
</tree>
</field>
</page>
</notebook>
</group>
</sheet>
</form>
</field>
</record>
<record id="plant_action" model="ir.actions.act_window">
<field name="name">plant</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">db.plant</field>
<field name="view_mode">tree,form</field>
</record>
</data>
and his child model (Data_source):
class DataSource(models.Model):
_name = 'db.data_source'
_description = 'db.data_source'
name = fields.Char("Name", required=True)
plant_id = fields.Many2one('db.plant', name="Plant", required=True)
data_source view:
<odoo>
<data>
<record id="data_source_list_view" model="ir.ui.view">
<field name="name">db.data_source.view</field>
<field name="model">db.data_source</field>
<field name="arch" type="xml">
<tree>
<field name="name"/>
...
</tree>
</field>
</record>
<record id="data_source_form_view" model="ir.ui.view">
<field name="name">db.form.view</field>
<field name="model">db.data_source</field>
<field name="arch" type="xml">
<form>
<group>
<field name="name"/>
...
</group>
<group>
<field name="plant_id" domain="[('id', '=', context.default_plant_id)]" />
</group>
<group>
</group>
</form>
</field>
</record>
<record id="data_source_action" model="ir.actions.act_window">
<field name="name">Data Source</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">db.data_source</field>
<field name="view_mode">tree,form</field>
</record>
</data>
I know that the default_plant_id is received in the child because the plaint_id domain is filtering just my plant_id but I am not able to set it automatically as a default value. I need to click on the list and select my plant_id (which is the only element in the list).
I also tried adding an #api.on_change("plant_id") in the child (data_source) in two ways:
Option 1:
#api.onchange('plant_id')
def onchange_plant_id(self):
if self.plant_id:
context = dict(self._context or {})
return [(0,0,{'plant_id': context.get('default_plant_id')})]
giving me this exception:
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/albertocrespo/tools/odoo/odoo/http.py", line 640, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/Users/albertocrespo/tools/odoo/odoo/http.py", line 316, in _handle_exception
raise exception.with_traceback(None) from new_cause
AttributeError: 'list' object has no attribute 'get'
Option 2 (I am not sure what to send as a response, but the write statement is also not working):
#api.onchange('plant_id')
def onchange_your_many_to_one_field(self):
context = dict(self._context or {})
self.plant_id.write({'id':context.get('default_plant_id')})})
res = {}
return res
I need to auto-select my plant as an option in the data_source view before saving it.
There is no need of onchange method or anything on xml side - context level. You can simply use field plant_id in a relation one2many field data_source_ids.
Try with this:
data_source_ids = fields.One2many("db.data_source", "plant_id")
I've been building an Odoo 9.1 module with this tutorial. But the workflow I created by that instruction is not working (When I click on the Confirm button, nothing happens, no error log etc)
Here's the related code:
models.py (in Session class)
state = fields.Selection([
('draft', "Draft"),
('confirmed', "Confirmed"),
('done', "Done"),
])
#api.multi
def action_draft(self):
self.state = 'draft'
#api.multi
def action_confirm(self):
self.state = 'confirmed'
#api.multi
def action_done(self):
self.state = 'done'
academy.xml (header in the Session form)
<header>
<button name="action_draft" type="workflow"
string="Reset to draft"
states="confirmed,done"/>
<button name="action_confirm" type="workflow"
string="Confirm" states="draft"
class="oe_highlight"/>
<button name="action_done" type="workflow"
string="Mark as done" states="confirmed"
class="oe_highlight"/>
<field name="state" widget="statusbar"/>
</header>
session_workflow.xml
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<record model="workflow" id="wkf_session">
<field name="name">OpenAcademy sessions workflow</field>
<field name="osv">academy.session</field>
<field name="on_create">True</field>
</record>
<record model="ir.actions.server" id="set_session_to_draft">
<field name="name">Set session to Draft</field>
<field name="model_id" ref="model_academy_session"/>
<field name="code">
model.search([('id', 'in', context['active_ids'])]).action_draft()
</field>
</record>
<record model="workflow.activity" id="draft">
<field name="name">Draft</field>
<field name="wkf_id" ref="wkf_session"/>
<field name="flow_start" eval="True"/>
<field name="kind">dummy</field>
<field name="action"></field>
<field name="action_id" ref="set_session_to_draft"/>
</record>
<record model="ir.actions.server" id="set_session_to_confirmed">
<field name="name">Set session to Confirmed</field>
<field name="model_id" ref="model_academy_session"/>
<field name="code">
model.search([('id', 'in', context['active_ids'])]).action_confirm()
</field>
</record>
<record model="workflow.activity" id="confirmed">
<field name="name">Confirmed</field>
<field name="wkf_id" ref="wkf_session"/>
<field name="kind">dummy</field>
<field name="action"></field>
<field name="action_id" ref="set_session_to_confirmed"/>
</record>
<record model="ir.actions.server" id="set_session_to_done">
<field name="name">Set session to Done</field>
<field name="model_id" ref="model_academy_session"/>
<field name="code">
model.search([('id', 'in', context['active_ids'])]).action_done()
</field>
</record>
<record model="workflow.activity" id="done">
<field name="name">Done</field>
<field name="wkf_id" ref="wkf_session"/>
<field name="kind">dummy</field>
<field name="action"></field>
<field name="action_id" ref="set_session_to_done"/>
</record>
<record model="workflow.transition" id="session_draft_to_confirmed">
<field name="act_from" ref="draft"/>
<field name="act_to" ref="confirmed"/>
<field name="signal">confirm</field>
</record>
<record model="workflow.transition" id="session_confirmed_to_draft">
<field name="act_from" ref="confirmed"/>
<field name="act_to" ref="draft"/>
<field name="signal">draft</field>
</record>
<record model="workflow.transition" id="session_done_to_draft">
<field name="act_from" ref="done"/>
<field name="act_to" ref="draft"/>
<field name="signal">draft</field>
</record>
<record model="workflow.transition" id="session_confirmed_to_done">
<field name="act_from" ref="confirmed"/>
<field name="act_to" ref="done"/>
<field name="signal">done</field>
</record>
<record model="workflow.transition" id="session_auto_confirm_half_filled">
<field name="act_from" ref="draft"/>
<field name="act_to" ref="confirmed"/>
<field name="condition">taken_seats > 50</field>
</record>
</data>
</openerp>
What went wrong?
change button name according to the signal value
your code:
<field name="signal">draft</field>
<button name="action_draft"...
change "action_draft" to "draft"
when the button is clicked, it would send button name as a signal, so they must be the same.
I want to few custom field in mrp.bom table in order to calculate the real row material consumption starting from drawing dimensions.
here is my .py code
from osv import osv, fields
class mrp_bom(osv.osv):
_inerhit = 'mrp.bom'
#_name = 'mrp.bom'
_columns = {
'Residuo_barra': fields.float(string='Residuo Barra', required=False),
'Sfrido': fields.float(string='Sfrido mm', required=False),
'L_barra': fields.float(string='lunghezza barra mm', required=False),
'L_pezzo_a_disegno': fields.float(string='L a disegno in mm', required=False),
'L_pezzo_calcolata': fields.float(string='Lunghezza calcolata', required=False),
}
_defaults = {
'Residuo_barra': 300.0,
'Sfrido': 4.0,
'L_barra': 3000.0,
}
def button_Calcola(self, cr, uid, ids, L_pezzo_a_disegno, Residuo_barra, Sfrido, L_barra, conext=None):
#calcola il consumo effettivo della barra
barra_utile = L_barra - Residuo_barra
numero_pezzi = int(barra_utile / (L_pezzo_a_disegno + Sfrido))
res = {
'L_pezzo_calcolata': (L_barra / numero_pezzi)
}
return {'value': res}
mrp_bom()
and here is the .xml
<record id="mrp_bom_tree_view" model="ir.ui.view">
<field name="name">mrp.bom.tree</field>
<field name="model">mrp.bom</field>
<field name="inherit_id" ref="mrp.mrp_bom_tree_view"/>
<field name="arch" type="xml">
<field name="product_id" position="after">
<field name="L_pezzo_calcolata" />
</field>
</field>
</record>
<record id="mrp_bom_component_tree_view" model="ir.ui.view">
<field name="name">mrp.bom.component.tree</field>
<field name="model">mrp.bom</field>
<field name="inherit_id" ref="mrp.mrp_bom_component_tree_view"/>
<field name="arch" type="xml">
<field name="product_id" position="after">
<field name="L_pezzo_calcolata" />
</field>
</field>
</record>
<record id="mrp_bom_form_view" model="ir.ui.view">
<field name="name">mrp.bom.form</field>
<field name="model">mrp.bom</field>
<field name="inherit_id" ref="mrp.mrp_bom_form_view"/>
<field name="arch" type="xml">
<xpath expr="//field[#name='bom_lines']/tree" position="inside" >
<field name="Residuo_barra" />
<field name="Sfrido" />
<field name="L_barra" />
<field name="L_pezzo_a_disegno" />
<field name="L_pezzo_calcolata" />
</xpath>
</field>
</record>
</data>
during the installation process I get :
ValidateError
Error occurred while validating the field(s) arch: Invalid XML for View Architecture!
If I check into the module structure Setting\Database structure\models\mrp_bom
the field have been added, But If I menage view in bom view the fields are not available!
Change the record id of inherited views so they are not the same.
For example:
<record id="mrp_bom_tree_view_add_field_L_pezzo_calcolata" model="ir.ui.view">
<field name="name">mrp_bom_tree_view_add_field_L_pezzo_calcolata</field>
<field name="model">mrp.bom</field>
<field name="inherit_id" ref="mrp.mrp_bom_tree_view"/>
<field name="arch" type="xml">
<field name="product_id" position="after">
<field name="L_pezzo_calcolata" />
</field>
</field>
</record>
I need to create one2one relation in openerp7. I read many articles about this idea and I could to type the following code
problem is : that openerp7 does not send value from parent view (calculation) to child (container)
this my code
testproject.py:
from osv import fields,osv
class container(osv.osv):
_name='container'
_columns={
'calculation_id': fields.many2one('calculation','Calculation'),
'name': fields.char('Name', size=32),
}
container()
class calculation(osv.osv):
_name='calculation'
_columns={
'container_id': fields.many2one('container','Container'),
'namefull': fields.char('Name Full', size=32),
}
calculation()
xml code:
<?xml version="1.0"?>
<openerp>
<data>
<record model="ir.ui.view" id="view_container_form">
<field name="name">container.form</field>
<field name="model">container</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Container">
<field name="name" select="1"/>
<field name="calculation_id" context="{'default_container_id': active_id}" />
</form>
</field>
</record>
<record model="ir.actions.act_window" id="action_container">
<field name="name">Container</field>
<field name="res_model">container</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem name="Container/Container" id="menu_container"/>-->
<menuitem name="Container" id="menu_container_item" parent="menu_container" action="action_container"/>
<record model="ir.ui.view" id="view_calculation_form">
<field name="name">calculation.form</field>
<field name="model">calculation</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Calculation">
<field name="namefull" />
<field name="container_id" />
</form>
</field>
</record>
<record model="ir.actions.act_window" id="action_calculation">
<field name="name">Calculation</field>
<field name="res_model">calculation</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem name="Calculation" id="menu_calculation_item" parent="menu_container" action="action_calculation"/>
</data>
</openerp>
You are correct, OpenERP will not move data around for you -- you will need to modify your Python code to do it.
Oh, and you should name your tables with your model as well -- I'll use a fake model name of my_model:
class calculation(osv.Model):
_name = 'my_model.calculation'
_columns = {
'container_id' fields.many2one('my_model.container', 'Container'),
'namefull': fields.char('Name Full', size=32),
}
def create(self, cr, uid, values, context=None):
new_id = super(calculation, self).create(cr, uid, values, context=context)
self.pool.get('my_model.container').create(cr, uid, {'calculation_id':new_id, 'name':values['namefull'])
return new_id
And something similer to write() in case namefull is updated.
I have a form with a lot of fields and with two button (Save and Close and Save and New)
when Save and New button is clicked I want those previously entered fields value to be displayed.
Thank You!
OK, my first solution (using context) didn't work out :-( but i tried another way, i dont like it very much, but it could help you out.
following my example .py:
from openerp.osv import orm, fields
class object_one(orm.Model):
_name = "object.one"
_columns = {
'name':fields.char('Name', size=128, required=True),
'many_ids':fields.many2many('object.many',string="Many Objects")
}
class object_many(orm.Model):
_name = "object.many"
_columns = {
'name':fields.char('Name', size=128, required=True),
'sel':fields.selection([('1','One'),
('2','Two'),
('3','Three')],
string="Selection", required=True),
}
def _get_sel(self, cr, uid, context={}):
many_id = self.search(cr, uid, [('create_uid','=',uid)], context=context, order="create_date desc", limit=1)
if many_id:
many = self.browse(cr, uid, many_id[0], context)
return many.sel
return False
_defaults = {
'sel':_get_sel
}
following my example .xml:
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<record id="one_form" model="ir.ui.view">
<field name="name">one form view</field>
<field name="model">object.one</field>
<field name="arch" type="xml">
<form version="7.0" string="">
<group>
<field name="name" />
<field name="many_ids" />
</group>
</form>
</field>
</record>
<record id="one_tree" model="ir.ui.view">
<field name="name">one tree view</field>
<field name="model">object.one</field>
<field name="arch" type="xml">
<tree version="7.0" string="">
<field name="name" />
</tree>
</field>
</record>
<record id="many_form" model="ir.ui.view">
<field name="name">many form view</field>
<field name="model">object.many</field>
<field name="arch" type="xml">
<form version="7.0" string="">
<group>
<field name="name" />
<field name="sel" />
</group>
</form>
</field>
</record>
<record id="many_tree" model="ir.ui.view">
<field name="name">many tree view</field>
<field name="model">object.many</field>
<field name="arch" type="xml">
<tree version="7.0" string="">
<field name="name" />
<field name="sel" />
</tree>
</field>
</record>
<record model="ir.actions.act_window" id="one_tree_action">
<field name="name">One Objects</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">object.one</field>
<field name="view_type">form</field>
<field name="view_mode">form,tree</field>
<field name="view_id" ref="one_tree" />
</record>
<record model="ir.actions.act_window" id="many_tree_action">
<field name="name">Many Objects</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">object.many</field>
<field name="view_type">form</field>
<field name="view_mode">form,tree</field>
<field name="view_id" ref="many_tree" />
</record>
<menuitem name="Testing Menu" id="testing_menu" />
<menuitem name="Sub Menu" parent="testing_menu" id="sub_menu" />
<menuitem action="one_tree_action" name="One Menu" parent="sub_menu" id="one_menu" />
<menuitem action="many_tree_action" name="Many Menu" parent="sub_menu" id="many_menu" />
</data>
</openerp>
you will see, every many-object created by an user, will have the last selection (sel) get from db. so its more of a workaround for your problem.
hope this will help you.