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()

  • 37
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子龙烜

坦克大战系列,手把手带你实现

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值