Hi I have to update a product data with a new module that add some features to product.product model but when I try to install the new module data it give me a error because its trying to create a new product and not to update the existing product
example: product record
<record model="product.product" id="product_product_11109613400">
<field name="list_price">0.07388597</field>
<field name="standard_price">0.0739</field>
<field name="default_code">11109613400</field>
<field name="name">LAMP</field>
<field name="categ_id" ref="product_category_categoria_06"/>
<field name="uom_po_id" ref="product.product_uom_unit"/>
<field name="uom_id" ref="product.product_uom_unit"/>
<field name="sale_ok" eval="False"/>
<field name="purchase_ok" eval="True"/>
</record>
the update xml:
<record model="product.product" id="product_product_11109613400">
<field name="hazard_ids" eval="[(4, ref('product_safety_advice_r4'))]"/>
</record>
I would look in ir_model_data and see what the name field is set to for the original create and that might give you a clue as to why the update is failing. If that doesn't work you could use a function that runs when the module is installed/updated.
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<function model="product.product" name="do_foo" />
</data>
</openerp>
and in product.product override
#api.model
def do_foo(self):
...
Related
I'm trying to add some demo data to a module
one of the models extends res.partner and has a create method (it overrides it from the parent)
The create method does 2 things
it checks that the current user is in some groups
it populates a serial number field with a value (calculated with a sequence)
This is the code
#api.model
def create(self, vals_list):
if not self.env.user.has_group('my-project.group_super') and not self.env.user.has_group(
'my-project.group_some_other_group'):
raise ValidationError(
f"Warning! You have no permission to create a patient. Contact your administrator")
vals_list['patient_number'] = self.env['ir.sequence'].next_by_code('my-project.res.partner')
return super(Patient, self).create(vals_list)
please, note the ValidationError in this method. The message it reports is
Warning! You have no permission to create a patient. Contact your
administrator
in the xml file I'm using to add demo data to this model I have this record
<record id="johnsmith" model="res.partner">
<field name="active" eval="True"></field>
<field name="is_patient" eval="True"></field>
<field name="company_type">person</field>
<field name="firstname">John</field>
<field name="lastname">Smith</field>
<field name="phone">+39 345 345 345</field>
<field name="email">asdfasdfasd</field>
<field name="birthdate">1999-02-17</field>
<field name="place_of_birth">Milan</field>
<field name="fiscalcode">IUCGIUCHEUIOHD38</field>
<field name="city">Milan</field>
<field name="country_id" ref="base.it"></field>
<field name="gender">male</field>
<field name="type">contact</field>
</record>
This record mostly works.
Meaning, it shows up as a demo record when I load demo data.
I say "mostly" because the field patient-number is not populated
But then I copy and paste this record and I only change the id to johnsmith2, like this
<record id="johnsmith2" model="res.partner">
<field name="active" eval="True"></field>
<field name="is_patient" eval="True"></field>
<field name="company_type">person</field>
<field name="firstname">John</field>
<field name="lastname">Smith</field>
<field name="phone">+39 345 345 345</field>
<field name="email">asdfasdfasd</field>
<field name="birthdate">1999-02-17</field>
<field name="place_of_birth">Milan</field>
<field name="fiscalcode">IUCGIUCHEUIOHD38</field>
<field name="city">Milan</field>
<field name="country_id" ref="base.it"></field>
<field name="gender">male</field>
<field name="type">contact</field>
</record>
this second record doesn't work
when installing the module in a database with demo data enabled, I get the error message in the body of the create method override
Warning! You have no permission to create a patient. Contact your
administrator
in the log and the loading of demo data fails
It seems that the first record is not being processed by the create method override, the second one is
what is going on here ?
EDIT
I'm adding the definition of user groups as this came up in the comments
These are the 2 groups mentioned in the create method
<record id="my_project" model="ir.module.category">
<field name="name">My Project</field>
<field name="description">Users groups for My project</field>
</record>
[...]
<record id="group_super" model="res.groups">
<field name="name">Supervisor</field>
<field name="category_id" ref="my-project.my_project"/>
</record>
[...]
<record id="group_some_other_group" model="res.groups">
<field name="name">Some Other Group</field>
<field name="category_id" ref="my-project.my_project"/>
</record>
These are the relevant lines in the ir.model.access.csv file
sc_study_partner_super,res.partner.super,model_res_partner,group_super,1,1,1,1
[...]
sc_study_partner_some_other_role,res.partner.some_other_role,model_res_partner,group_some_other_group,1,1,1,1
The demo/patients.xml file does not contain the no update flag
You can use self.env.is_admin() to check if its runned by administrator environment
Also, it is common to add rights to the super user and the admisstrator:
<record model="res.users" id="base.user_root">
<field eval="[(4,ref('my-project.group_super'))]" name="groups_id"/>
</record>
<record model="res.users" id="base.user_admin">
<field eval="[(4,ref('my-project.group_super'))]" name="groups_id"/>
</record>
How can I add inputs (code and name) in the below example for module hr_payroll?
For example: input name is "Stimulation", code = "STM"
<record id="hr_rule_taxable" model="hr.salary.rule">
<field name="name">Gross</field>
<field name="sequence" eval="100"/>
<field name="code">GROSS</field>
<field name="category_id" ref="hr_payroll.GROSS"/>
<field name="condition_select">none</field>
<field name="amount_select">code</field>
<field name="amount_python_compute">result = categories.BASIC + categories.ALW</field>
</record>
Are you asking how to override parts of this data in your module's XML?
If so, it can be done by using <module_name>.<view_id> to override certain fields from the same record, such as:
<record id="hr_payroll.hr_rule_taxable" model="hr.salary.rule">
<field name="name">Stimulation</field>
<field name="code">STM</field>
</record>
Note: This does not work if the original record is in a <data noupdate="1"> block.
In parent module I have something likes :
<record id="filter_hr_timesheet_report_internal_timesheets" model="ir.filters">
<field name="name">Internal Timesheet</field>
<field name="model_id">hr.timesheet.report</field>
<field name="domain">[('state','=','done'), ('date','<=', time.strftime('%Y-%m-%d')),('date','>=',time.strftime('%Y-%m-01'))]</field>
<field name="user_id" eval="False"/>
<field name="context">{'group_by': ['user_id'], 'col_group_by': ['department_id'], 'measures': ['time']}</field>
</record>
and I want to change group_by in context field but can't find any document for model "ir.filters"
Update:
I resolved it by myself by :
<record id="hr_timesheet_sheet.filter_hr_timesheet_report_internal_timesheets" model="ir.filters">
<field name="context">{'group_by': ['employee_id'], 'col_group_by': ['department_id'], 'measures': ['time']}</field>
</record>
We can access record id with module name and we can update any attribute as per our requirement.
For example:
module_name.xml_record_id
Try with following code:
<record id="hr_timesheet_sheet.filter_hr_timesheet_report_internal_timesheets" position="attributes">
<attribute name="context">NEW CONTEXT WILL COME HERE</attribute>
</record>
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.
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!")