odoo开发规范

2024-06-22 06:36
文章标签 开发 odoo 规范

本文主要是介绍odoo开发规范,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

命名规范

所有模型用tx开头:tx业务,如tx_study

文件名规范

models/<main_model>.py 

views/<main_model>_views.xml 

views/<main_model>_menu.xml 

views/<main_model>_templates.xml 

wizard/<main_transient>.py 

wizard/<main_transient>_views.py 

security/<main_model>_security.xml 

data/<main_model>__data.xml 

report/<print_report_name>_reports.py 

report/<print_report_name>_templates.xml 

  •  下面以tx_study为例:

models/tx_study.py views/tx_study_views.xml 

views/tx_study_menu.xml 

views/tx_study_templates_xml 

wizard/tx_study_wizard.py 

wizard/tx_study_wizard.xml 

security/tx_study_security.xml 

data/tx_study_data.xml 

report/tx_study_something_report.py 

report/tx_study_something_templates.xml

 xml_id命名

  • 视图(view):<model_name>_ view _ <view_type>,view_type可能的取值有:kanban, form, tree, search等 
  • 动作(action):action _ <model_name> 
  • 菜单(menu): menu _ <model_name> 
  • 权限分类(category): module_category_ <category_name> 
  • 权限组(group):<model_name>_ group _ <group_name>,group_name为组名,可能的 取值包括:user, manager。在权限细分的情况下,可以细分为增删改查,并且具体到页面上 的按钮等。 
  • 记录规则(rule) : <model_name>_ rule _ <concerned_group>,concerned_group 是相关权 限组的缩写,如 (‘user’ for the ‘model_name_group_user’). 报表(report):report_ <print_report_name>
  • 所有的菜单项统一放在menu.xml文件中
  • 每个菜单项都需要配置权限组  
  • 菜单项要设置sequence,sequence的间隔预留大一些,如10、20、30... ,sequence越小菜单越靠前
  • 每个模型都要配置search视图
  • 动作窗口要指定视图,特别是在模型在同类型的视图中不止有一个时  

例如

<record id="tx_study_view_tree" model="ir.ui.view">... </record> <record id="tx_study_view_form" model="ir.ui.view"> ... </record> <record id="tx_study_view_search" model="ir.ui.view">... </record> <record id="action_tx_study" model="ir.actions.act_window"> .... <field name="view_ids" eval="[(5, 0, 0), (0, 0, {'view_mode': 'tree', 'view_id': ref('tx_study_view_tree')}), 指定tree视图 (0, 0, {'view_mode': 'form', 'view_id': ref('tx_study_view_form')})]"/> 指定form视图 <field name="search_view_id" ref="tx_study_view_search"/> 指定search视图 </record> <menuitem id="menu_tx_study" name="Study" sequence="10" action="action_tx_study" groups="base.group_user"/> <record id="reports_tx_study_something" model="ir.actions.report">... </record> <record model="ir.module.category" id="module_category_Tx_Study">... </record> <record model="res.groups" id="tx_study_user">... </record> <record id="tx_study_rule_public" model="ir.rule">... </record>

权限细分

<!--增删改查--> <record model="res.groups" id="tx_study_read"> <field name="name">Study Read</field> ...</record><record model="res.groups" id="tx_study_write"> <field name="name">Study Write</field>... </record>  <record model="res.groups" id="tx_study_create"> <field name="name">Study Create</field>... </record> <record model="res.groups" id="tx_study_unlink"> <field name="name">Study Delete</field>... </record><!--按钮--> <record model="res.groups" id="tx_study_btn_action_submit"> <field name="name">Study btn Submit</field> ... </record> <button name="action_submit" string="Submit" class="oe_highlight" confirm="Are you sure you want to submit?" type="object" groups="tx_study_btn_action_submit" /> <!--page--> <record model="res.groups" id="tx_study_page_basic_info"> <field name="name">Study Page Basic Info</field> </record> ... <page name="basic" string="Basic "groups="tx_study_page_basic_info"> ... </page> 

 

继承xml命名

<record id="tx_study_view_tree_inherit" model="ir.ui.view"> <field name="inherit_id" ref="tx_study.tx_study_view_tree"> ... </record>

权限文件编写

ir.model.access.csv文件:文件名固定

1. id:权限的ID (access+模型名+角色名) 

2. name:权限的name (模型+角色名) 

3. model_id:id;模型的ID (model_+模型名) 

4. group_id:id;群组的ID (模型名+group+用户名) 

5. perm_read:查看权限 

6. perm_write:修改权限 

7. perm_create:增加数据权限

8. perm_unlink:删除权  

id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_tx_study_user,tx.study.user,model_tx_study,group_tx_study_user,1,1,1,1权限细分: id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink 
access_01,tx.study,model_tx_study,tx_study_read,1,0,0,0 
access_02,tx.study,model_tx_study,tx_study_write,0,1,0,0 
access_03,tx.study,model_tx_study,tx_study_create,0,0,1,0 
access_04,tx.study,model_tx_study,tx_study_unlink,0,0,0,1  

manifest清单文件编写

模块名,以tx_开头 

author:作者,固定为 广东同欣智能科技有限公司 

website:网址,固定为 Home | 同欣精益 (beauty-lean.com)

version:版本,固定为 当前odoo开发版本 

summary:模块简述,尽可能简洁 

description:模块描述,尽可能全面描述模块功能 

maintainer:运维者,开发者名称 

category:分类

{ 
'name': "tx_study", 'summary': """ study """, 'description': """ study """, 'author': "广东同欣智能科技有限公司", 'website': "https://www.beauty-lean.com/", 'maintainer': "your name" 'category': '', 'version': '16.0.1', 'installable': True, 'depends': ['base'], 'data': [ 'security/ir.model.access.csv', 'views/tx_study_views.xml', 'views/tx_study_menu.xml',  ... ], ... }

 odoo编码规范

官方编码定义顺序

在模型属性中,顺序应为 

1. 私有属性 _name _description _inherit _sql_constraints 

2. 默认方法和 default_get 

3. 字段声明 

4. 计算方法、反向方法和搜索方法的顺序与字段声明相同 

5. 选择方法(用于返回选择字段的计算值的方法) 

6. 约束方法 () 和 onchange 方法 ( @api.constrains @api.onchange ) 7. CRUD 方法(ORM 覆盖) 

8. 操作方法 

9. 最后,其他商业方法。  

One2Many, Many2Many字段一般以ids作为后缀如:sale_order_line_ids Many2One 一般以_id为后缀如:partner_id, user_id

class TxStudy(models.Model):_name = 'tx.study' _description = 'Tx Study' _order = 'id desc' def _default_name(self): ... name = field.Char(string='Name', default=_default_name) area = field.Float(string='Area', compute='_compute_area')... @api.depends('width', 'length') def _compute_area(self): ... @api.constrains('name') def _check_name(self): ... @api.onchange('name') def _onchange_name(self): ... def create(self, values):...  def action_validate(self): self.ensure_one() ...def do_something(self): ...

 继承model

class TxStudy(models.Model): _inherit = 'tx.study'...  

wizard向导:

class TxCreateStudyWizard(models.TransientModel):_name = "tx.create.study.wizard" _description = "Create Study" 

report报表  

class ReportTxStudySomething(models.AbstractModel): _name = 'report.tx_study.something'_description = "Study Something" def _get_report_values(self, docids, data=None): docs = self.env['tx.study'].sudo().browse(docids) docargs = { 'docs': docs, } return docargs

使用write进行赋值,而不是直接用等于。

# 隐式调用

write def action_to_done(self): 

self.state = 'done' 

# 推荐写法, 显式调用 

def action_to_done(self): self.write.({"state": 'done'})

使用ensure_one()确保是单实例方法  

def action_to_record(self): 

self.ensure_one()

这篇关于odoo开发规范的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1083553

相关文章

VSCode开发中有哪些好用的插件和快捷键

《VSCode开发中有哪些好用的插件和快捷键》作为全球最受欢迎的编程工具,VSCode的快捷键体系是提升开发效率的核心密码,:本文主要介绍VSCode开发中有哪些好用的插件和快捷键的相关资料,文中... 目录前言1、vscode插件1.1 Live-server1.2 Auto Rename Tag1.3

Agent开发核心技术解析以及现代Agent架构设计

《Agent开发核心技术解析以及现代Agent架构设计》在人工智能领域,Agent并非一个全新的概念,但在大模型时代,它被赋予了全新的生命力,简单来说,Agent是一个能够自主感知环境、理解任务、制定... 目录一、回归本源:到底什么是Agent?二、核心链路拆解:Agent的"大脑"与"四肢"1. 规划模

Python+wxPython开发一个文件属性比对工具

《Python+wxPython开发一个文件属性比对工具》在日常的文件管理工作中,我们经常会遇到同一个文件存在多个版本,或者需要验证备份文件与源文件是否一致,下面我们就来看看如何使用wxPython模... 目录引言项目背景与需求应用场景核心需求运行结果技术选型程序设计界面布局核心功能模块关键代码解析文件大

C++多线程开发环境配置方法

《C++多线程开发环境配置方法》文章详细介绍了如何在Windows上安装MinGW-w64和VSCode,并配置环境变量和编译任务,使用VSCode创建一个C++多线程测试项目,并通过配置tasks.... 目录下载安装 MinGW-w64下载安装VS code创建测试项目配置编译任务创建 tasks.js

一文详解Python如何开发游戏

《一文详解Python如何开发游戏》Python是一种非常流行的编程语言,也可以用来开发游戏模组,:本文主要介绍Python如何开发游戏的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、python简介二、Python 开发 2D 游戏的优劣势优势缺点三、Python 开发 3D

基于Python开发Windows自动更新控制工具

《基于Python开发Windows自动更新控制工具》在当今数字化时代,操作系统更新已成为计算机维护的重要组成部分,本文介绍一款基于Python和PyQt5的Windows自动更新控制工具,有需要的可... 目录设计原理与技术实现系统架构概述数学建模工具界面完整代码实现技术深度分析多层级控制理论服务层控制注

Java中的分布式系统开发基于 Zookeeper 与 Dubbo 的应用案例解析

《Java中的分布式系统开发基于Zookeeper与Dubbo的应用案例解析》本文将通过实际案例,带你走进基于Zookeeper与Dubbo的分布式系统开发,本文通过实例代码给大家介绍的非常详... 目录Java 中的分布式系统开发基于 Zookeeper 与 Dubbo 的应用案例一、分布式系统中的挑战二

基于Go语言开发一个 IP 归属地查询接口工具

《基于Go语言开发一个IP归属地查询接口工具》在日常开发中,IP地址归属地查询是一个常见需求,本文将带大家使用Go语言快速开发一个IP归属地查询接口服务,有需要的小伙伴可以了解下... 目录功能目标技术栈项目结构核心代码(main.go)使用方法扩展功能总结在日常开发中,IP 地址归属地查询是一个常见需求:

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版