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

相关文章

Python38个游戏开发库整理汇总

《Python38个游戏开发库整理汇总》文章介绍了多种Python游戏开发库,涵盖2D/3D游戏开发、多人游戏框架及视觉小说引擎,适合不同需求的开发者入门,强调跨平台支持与易用性,并鼓励读者交流反馈以... 目录PyGameCocos2dPySoyPyOgrepygletPanda3DBlenderFife

使用Python开发一个Ditto剪贴板数据导出工具

《使用Python开发一个Ditto剪贴板数据导出工具》在日常工作中,我们经常需要处理大量的剪贴板数据,下面将介绍如何使用Python的wxPython库开发一个图形化工具,实现从Ditto数据库中读... 目录前言运行结果项目需求分析技术选型核心功能实现1. Ditto数据库结构分析2. 数据库自动定位3

Django开发时如何避免频繁发送短信验证码(python图文代码)

《Django开发时如何避免频繁发送短信验证码(python图文代码)》Django开发时,为防止频繁发送验证码,后端需用Redis限制请求频率,结合管道技术提升效率,通过生产者消费者模式解耦业务逻辑... 目录避免频繁发送 验证码1. www.chinasem.cn避免频繁发送 验证码逻辑分析2. 避免频繁

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

PyQt5 GUI 开发的基础知识

《PyQt5GUI开发的基础知识》Qt是一个跨平台的C++图形用户界面开发框架,支持GUI和非GUI程序开发,本文介绍了使用PyQt5进行界面开发的基础知识,包括创建简单窗口、常用控件、窗口属性设... 目录简介第一个PyQt程序最常用的三个功能模块控件QPushButton(按钮)控件QLable(纯文本

基于Python开发一个图像水印批量添加工具

《基于Python开发一个图像水印批量添加工具》在当今数字化内容爆炸式增长的时代,图像版权保护已成为创作者和企业的核心需求,本方案将详细介绍一个基于PythonPIL库的工业级图像水印解决方案,有需要... 目录一、系统架构设计1.1 整体处理流程1.2 类结构设计(扩展版本)二、核心算法深入解析2.1 自

SpringBoot开发中十大常见陷阱深度解析与避坑指南

《SpringBoot开发中十大常见陷阱深度解析与避坑指南》在SpringBoot的开发过程中,即使是经验丰富的开发者也难免会遇到各种棘手的问题,本文将针对SpringBoot开发中十大常见的“坑... 目录引言一、配置总出错?是不是同时用了.properties和.yml?二、换个位置配置就失效?搞清楚加

Python中对FFmpeg封装开发库FFmpy详解

《Python中对FFmpeg封装开发库FFmpy详解》:本文主要介绍Python中对FFmpeg封装开发库FFmpy,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、FFmpy简介与安装1.1 FFmpy概述1.2 安装方法二、FFmpy核心类与方法2.1 FF

基于Python开发Windows屏幕控制工具

《基于Python开发Windows屏幕控制工具》在数字化办公时代,屏幕管理已成为提升工作效率和保护眼睛健康的重要环节,本文将分享一个基于Python和PySide6开发的Windows屏幕控制工具,... 目录概述功能亮点界面展示实现步骤详解1. 环境准备2. 亮度控制模块3. 息屏功能实现4. 息屏时间

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部