- 上传者: Administrator
- 上传时间:2023年11月19日 02时22分46秒
* Data Loading ** *** Odoo is mainly data-driven, and most operations greatly depend on data. In this section, we are going to discuss how to load da ......
Data Loading
Odoo is mainly data-driven, and most operations greatly depend on data. In this section, we are going to discuss how to load data on module installation - the different ways and more operations related to Odoo data. There are two ways to load data in Odoo
· Using XML files
· Using CSV files
Xml Files
Data records can be defined in Odoo using XML files. For that data XML files are to be placed. First, we have to create a directory inside the module called data. data.xml files are commonly created inside the directory data. Let’s discuss how to create partner data using XML files. For that, let us create a new module called custom_contacts, inside which creates a new directory called data. Inside the data directory, create an XML file named res_partner_data.xml.The record data can be loaded using an XML file by following the syntax below.
<record model="model_name" id="record_id">
<field name="field_name">field_data</field>
</record>
For creating a record, it is necessary to give values for all required fields. We can create a partner record using the below XML code. The record is created using the record tag. Two attribute values can be set for the record tag: id and model. ‘Id’ is the unique identifier for this XML record. ‘Model’ specifies in which model the record is being created. In this example, we are creating a new record in the Contacts model; hence the model is given as res.partner. Inside the record, each field value is given using the field tag as given below. The Name specifies the technical name of the field, and after that, the field value is given.
<odoo>
<data>
<record id="partner_1" model="res.partner">
<field name="name">Aby Wills</field>
<field name="phone">9865432344</field>
<field name="email">aby@example.com</field>
</record>
</data>
</odoo>
After creating this data.xml file add this inside __manifest__.py file as follows.
'data' : [
'data/res_partner_data.xml'
]
After configuring this, install the module custom_contacts. A partner data with the specified values will be created in the database after installing the module.
CSV Files
CSV files are used in case of large data loading, as using XML files may not be suitable. In Odoo source modules itself, the usage of CSV files can be seen. Mainly they are used to define access rights. There are some important points to remember when using CSV files.
· This file's name should be model_name.csv."ir.model.access.csv," for instance.ir.model.access is the model name for the access permissions.CSV files are also added inside the manifest.
· The first row of the CSV file will have the fields that need to be written into, and we also have a field id for the external ID (used for creation or updation)
Let’s see this with the use of an example. For that, consider the account.tax.group.csv.
id,name,country_id/id
tax_group_0,TVA 0%,base.lu
tax_group_3,TVA 3%,base.lu
tax_group_6,TVA 6%,base.lu
tax_group_8,TVA 8%,base.lu
tax_group_10,TVA 10%,base.lu
tax_group_12,TVA 12%,base.lu
tax_group_14,TVA 14%,base.lu
tax_group_15,TVA 15%,base.lu
tax_group_17,TVA 17%,base.lu
The first row specifies the fields and is followed by values. Hence on installing the module consisting of this CSV file, the corresponding records inside the model account.tax.group will be created.
No Update
Odoo features a property named No Update for the element. The values in the XML file will be produced again whenever a module is altered or upgraded according to this parameter value. In some cases, the data files' contents can not be changed. At that moment, the noupdate property value can be set to 1.
<odoo>
<data noupdate="1">
<record id="partner_1" model="res.partner">
<field name="name">Aby Wills</field>
<field name="phone">9865432344</field>
<field name="email">aby@example.com</field>
</record>
</data>
</odoo>
Consider the above example. Since the noupdate attribute value is set to 1, any changes in this xml record will not be effected during the further upgrade process of the custom_contacts module. All the properties will be only effected at the time of the installation process. Hence using the noupdate attribute, we can make sure during the development process that the record created using the XML file is not updated frequently.
Force Create
Like noupdate, the forcecreate is also an attribute of the tag. By using this attribute, we can make sure that the record is being created if it doesn’t exist. The forcecreate attribute is true by default. This also requires an external ID of the record. Consider the example given below.
<record forcecreate="True" id="decimal_payment" model="decimal.precision">
<field name="name">Payment Terms</field>
<field name="digits">6</field>
</record>
Here, if there is no record found in the model decimal.precision with external id decimal_payment, a new record will be created.
本文章从网上收集,如有侵权请联系tderp@tderp.com删除
- 微信扫一扫,一分也是爱:
-
服务原则及地区范围
宜兴通达网络科技有限公司,地处中国宜兴环科园内,是一家高新技术企业。公司在企业网络维护和企业信息化建设与咨询方面,有10多年经验。
我公司愿与客户一道,力求彻底解决客户问题!
我们不是在给企业提供“头痛医头、脚痛医脚”的暂时解决方案,而是在部署根本性安全与稳定服务!!
我们愿携手客户,建立企业IT规划;杜绝随意安装系统、软件等操作;力求共同维护有序、安全、稳定的网络办公环境!!!
IT服务,服务是根本,客户是上帝;我们提供快速响应、快速上门、快速排查,提供优质高效的服务!!!!
通达科技提供全国范围内的服务,服务形式包括远程协助、电话咨询、电子邮件咨询、传真咨询、问答平台的问题解决等。
宜兴地区提供上门服务:
- 市区服务:宜城街道、城北街道(屺亭街道)、新街街道、新庄街道、环科园、渚桥开发区
- 市郊服务:张渚镇、西渚镇、太华镇、徐舍镇、官林镇、杨巷镇、新建镇、和桥镇、高塍镇、万石镇、周铁镇、芳桥镇、丁蜀镇、湖父镇。