Odoo write to field not part of the current view - odoo

I want to update the field done_date which is part of the project.task form view when pulling a Kanban tile on the stage that is indicated as being the final stage.
My code below works fine if the field is part of the Kanban view but fails to write if the field is only part of the task form view and not part of the Kanban project view.
The done_date field should be written even without being part of the Kanban view.
models.py
# -*- coding: utf-8 -*-
from odoo import models, fields, api
class project_set_end_date(models.Model):
inherit = 'project.task.type'
last_stage = fields.Boolean(string="Final stage")
class project_set_end_date(models.Model):
_inherit = 'project.task'
#api.onchange('stage_id')
def _set_end_date(self):
if self.stage_id.last_stage:
self.date_finished = fields.Datetime.now()
views.py
<odoo>
<data>
<!-- explicit list view definition -->
<record model="ir.ui.view" id="project_set_end_date">
<field name="name">project.task.type.form</field>
<field name="model">project.task.type</field>
<field name="inherit_id" ref="project.task_type_edit"/>
<field name="arch" type="xml">
<xpath expr="//field[#name='fold']" position='after'>
<field name="last_stage"/>
</xpath>
</field>
</record>
</data>
</odoo>

Odoo doesn't write to a field that doesn't exist in the current view. So I suggest adding the field but with the attribute invisible = True to avoid showing it:
<field name="your_field" invisible="True"/>

Related

Trying to show custom product field on stock.inventory.line view

I'm trying to show custom product field on stock.inventory.line but keep getting error "Field stock.inventory.line.outdated cannot find dependency 'stock_move_ids' on model 'product.template'."
This is in my module manifest file under dependencies:
'depends': ['base', 'product', 'stock', 'sale', 'account', 'stock_account']
This is inside python file:
from odoo import models, fields
class StockInventoryLine(models.Model):
_inherit = 'stock.inventory.line'
product_id = fields.Many2one('product.template', string='Product')
instrument_field = fields.Many2one(related='product_id.instrument', string='Instrument')
This is in my XML file:
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<data>
<record id="view_stock_inventory_line_tree" model="ir.ui.view">
<field name="name">stock.inventory.line.tree</field>
<field name="model">stock.inventory.line</field>
<field name="inherit_id" ref="stock.view_stock_inventory_line_tree"/>
<field name="arch" type="xml">
<xpath expr="//field[#name='product_id']" position="after">
<field name="instrument_field"/>
</xpath>
</field>
</record>
</data>
</odoo>
I get that probably the error is with the model stock.inventory.line but I don't know what to use here.
The model stock.inventory.line doesn't exists any more in Odoo 15. it's replaced with stock.quant model.
If you are using Odoo 14 or previous versions, the product_id field in stock.inventory.line defined as Many2one from product.product and you redefined it to link it with product.template.
The product.product inheriting all fields from product.template so you can get instrument_field directly from product.product
Another thing, stock_move_ids defined in product.product and because of your overriding, Odoo couldn't find it in product.template
Please remove the product_id from your code as below.
from odoo import models, fields
class StockInventoryLine(models.Model):
_inherit = 'stock.inventory.line'
instrument_field = fields.Many2one(related='product_id.instrument',
string='Instrument')

How do I add multiple models to one view?

I'm trying to use three different models in one view. I've created a new model that inherits the models which seems to work fine.
from openerp import models, fields, api
class ProjectNote(models.Model):
_name = "triangle.project.note"
_inherit = ["note.note", "project.project", "triangle.note"]
My problem is in the view. I use my new model as the model and inherit a view from project.
<record id="view_project_notes_form" model="ir.ui.view">
<field name="name">triangle.project.note.form</field>
<field name="model">triangle.project.note</field>
<field name="inherit_id" ref="project.edit_project"/>
<field name="arch" type="xml">
<data>
<xpath expr="//field[#name='privacy_visibility']" position="replace">
<h2>
<field name="title" placeholder="Title"/>
</h2>
</xpath>
</data>
</field>
</record>
I don't get any errors but my field is not being added.
Any help is appreciated!
If you're trying to open a project.project view and wondering why there is no field title in it: there can't be. You aren't extending the project view for model project.project but are defining a form view for your model triangle.project.note which inherits the project view.
So the project views are untouched, you've just created the first form view for your new model.

Error while adding custom field using module: ParseError: "arch" while parsing file

I am trying to add a custom field to Odoo 9 res.partner model using module. I have used scoffold command to generate the module files and added following code to models.py and views.xml.
models/models.py
from openerp import models, fields, api
class SeicoPartner(models.Model):
_name = 'res.partner'
_inherit = 'res.partner'
no_of_ac = fields.Integer('No of AC', default=0)
review = fields.Char('Company Review')
views/views.xml
<openerp>
<data>
<record id="res_partner_field_ac" model="ir.ui.view">
<field name="no_of_ac">10</field>
</record>
</data>
</openerp>
Upon installation of this module from Apps screen, I got the following error:
Traceback (most recent call last):
...
File "C:\Program Files (x86)\Odoo 9.0-20160719\server\openerp\addons\base\ir\ir_ui_view.py", line 344, in create
ParseError: "arch" while parsing file:///C:/Program%20Files%20(x86)/Odoo%209.0-20160719/server/openerp/addons/mymodule1/views/views.xml:4, near
<record id="res_partner_field_ac" model="ir.ui.view">
<field name="no_of_ac">10</field>
</record>
From the Settings -> Database Structure -> Fields I can see that res.partner has the no_of_ac field, but the field is not visible while editing any customer details.
That's because you're missing the arch field with describes the type of view (either xml or html), in most cases xml is just fine,
You're also missing the model name, the view name, so odoo doesn't know which model your view belongs to. you also have to specify the existing model form you want to over-ride and the position where you want the new field to be, in this case i used an xpath expression to display the field after the website field in the parent view, it can be anywhere you want it to be.
<openerp>
<data>
<record id="res_partner_field_ac" model="ir.ui.view">
<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="//field[#name='website']" position="after">
<field name="no_of_ac" />
</xpath>
</field>
</record>
</data>
</openerp>
also you don't need to specify _name if you just want to extend a model and add extra fields to it, so change your model code to this
from openerp import models, fields, api
class SeicoPartner(models.Model):
_inherit = 'res.partner'
no_of_ac = fields.Integer('No of AC', default=0)
review = fields.Char('Company Review')
If you want to add a data record you should use res.partner as model and define the required fields:
<record id="res_partner_field_ac" model="res.partner">
<field name="no_of_ac">10</field>
<field name="name">NEW PARTNER NAME</field>
<!-- define required fields -->
</record>
To define a view, take a look at Odoo views
You are inserting a new record in ir.ui.view data model.
<openerp>
<data>
<record id="res_partner_field_ac" model="ir.ui.view">
<field name="no_of_ac">10</field>
</record>
</data>
</openerp>
But you want to insert data to : res.partner
<openerp>
<data>
<record id="res_partner_field_ac" model="res.partner">
<field name="name">a name here because it's required when the field type != 'contact' </field>
<field name="no_of_ac">10</field>
</record>
</data>
</openerp>
NOTE :you encountered a problem because arch is a requrired field inir.ui.view model.

Odoo header buttons missing

I'm having a problem with the create button appearing in tree view. Neither does the next or previous buttons appear in the form view. However, the data is being retrieved from the database.
Tree form with missing buttons
The module I'm trying to make is an extended module of the human resource module, like the included HR attendance module. The extended module doesn't inherit anything and security hasn't been added yet. Only a menu item is added to the main module.
A module I previously created by inheriting the main HR module created the buttons as expected.
Expected outcome(different module)
training.py:
from openerp import fields, models, api
class ew_training(models.Model):
_name = 'hr.training'
var = fields.Char( string='variable')
training_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<!-- Main Submenu -->
<menuitem id="menu_training_tree" action="action_view_training"
parent="hr.menu_hr_main" sequence="6"/>
<record id="action_view_training" model="ir.actions.act_window">
<field name="name">Training</field>
<field name="res_model">hr.training</field>
<field name="view_type">tree</field>
<field name="view_mode">tree,form</field>
</record>
<record id="view_training_tree" model="ir.ui.view">
<field name="name">hr.training.tree</field>
<field name="model">hr.training</field>
<field name="arch" type="xml">
<tree>
<field name="var"/>
</tree>
</field>
</record>
<record id="view_training_form" model="ir.ui.view">
...
</record>
</data>
</openerp>
Please try avoid using the old API
EDIT
This should be work if you are trying to call differents views in differents actions.
The problem is not the button create, the problem is that you are not calling the tree view on your action action_view_training, try adding this line after view_mode:
<field name="view_id" ref="view_training_tree"/>
EDIT
To solve your case you only need to change in the view_type, you should use form:
<record id="action_view_training" model="ir.actions.act_window">
<field name="name">Training</field>
<field name="res_model">hr.training</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>
It should be work perfectly!!! I hope this could be helpful for you.
Just for Information.
in action view_type tree can be used when you want to create hierarchical view, It will not give you ability to create or update record. view of company structure in Odoo is the example of view type tree.
and view_type to form in action will allow you to create normal tree, form view with ability to Create, Update, Duplicate, Delete.
Hope this helps.

How do I get the value from a form field using Odoo?

I have this field in a form view:
<field name="value"/>
I want to get the value from the field when someone wants to add a new value, the way that I might do $_GET['value'] in PHP.
Simple example:
I want, when the user inserts a value, for the program to check if it is greater than sum of all values and print an error message like:
Cannot add the value because it is greater than the sum of all values
I've written this so far:
view.xml
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="action_test" model="ir.actions.act_window">
<field name="name">Insert</field>
<field name="res_model">test.odoo</field>
<field name="view_mode">form,tree</field>
</record>
<record id="view_tree_test" model="ir.ui.view">
<field name="name">Inset.tree</field>
<field name="model">test.odoo</field>
<field name="arch" type="xml">
<tree string="T">
<field name="create_uid"/>
<field name="value" sum="Total Sum"/>
</tree>
</field>
</record>
<record id="view_from_test" model="ir.ui.view">
<field name="name">Inset.form</field>
<field name="model">test.odoo</field>
<field name="arch" type="xml">
<form string="T_Form">
<group>
<field name="value"/>
</group>
</form>
</field>
</record>
<menuitem name="Test Module" id="main_menu_test" sequence="3"/>
<menuitem name="TEST" id="sub_menu" parent="main_menu_test"/>
<menuitem action="action_test" id="action_menu" parent="sub_menu" />
</data>
</openerp>
test.py
from openerp import models
from openerp import fields
class test(models.Model):
_name = "test.odoo"
value = fields.Integer()
You cannot think about form view in odoo in isolation from the database layer.
The data from the form is immediately (and automatically) saved in the database. You can later access it using the ORM methods.
I don't know what exactly you want to achieve, so it's hard for me to give you a concrete example. Every form view is associated with a single ORM model. If you want to do do something with the data immediately before/after it is saved, you would usually subclass the ORM model and overwrite one of its methods.
class Foo(models.Model):
_inherit = 'other.foo'
#api.model
def create(self, vals):
record = super(Foo, self).create(vals)
print "A new Foo with name={} and bar={} has been created!".format(
record.name,
record.bar,
)
return record
This is how you validate a form:
from openerp import models, fields, api, exceptions
class test(models.Model):
_name = 'test.odoo'
value = fields.Integer()
#api.one
#api.constrains('value')
def _check_value(self):
if self.value > 25:
raise exceptions.ValidationError("The value is too large!")