新增模块
python3 ./odoo-bin scaffold group_send ./addons
会增加一个文件夹
配置模块
核心在于__manifest__.py
# -*- coding: utf-8 -*-
{
'name': "Group Send",
'summary': "Short (1 phrase/line) summary of the module's purpose",
'description': """
Long description of module's purpose
""",
'author': "My Company",
'website': "https://www.yourcompany.com",
# Categories can be used to filter modules in modules listing
# Check https://github.com/odoo/odoo/blob/15.0/odoo/addons/base/data/ir_module_category_data.xml
# for the full list
'category': 'Uncategorized',
'version': '0.1',
# any module necessary for this one to work correctly
'depends': ['base'],
# always loaded
'data': [
'security/ir.model.access.csv',
'views/views.xml',
'views/templates.xml',
],
# only loaded in demonstration mode
'demo': [
'demo/demo.xml',
],
'installable': True,
'application': True,
'license': 'LGPL-3',
}
定义模型
models/user.py
from odoo import models, fields
class User(models.Model):
_name = 'group_send.user'
_description = 'Group Send User'
name = fields.Char(string='Name', required=True)
is_email = fields.Boolean(string='IsEmail')
is_tg = fields.Boolean(string='IsTG')
email = fields.Char(string='Email')
tg = fields.Char(string='tg')
定义视图
修改view/views.xml
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<!-- 用户表单视图 -->
<record id="group_send_form_view" model="ir.ui.view">
<field name="name">group.send.user.form</field>
<field name="model">group_send.user</field>
<field name="arch" type="xml">
<form string="Group Send Users">
<sheet>
<group>
<field name="name"/>
<field name="is_email"/>
<field name="is_tg"/>
</group>
<notebook>
<page string="Contact Information">
<field name="email" colspan="4" />
<field name="tg" colspan="4" />
</page>
</notebook>
</sheet>
</form>
</field>
</record>
<!-- 用户列表视图 -->
<record id="group_send_tree_view" model="ir.ui.view">
<field name="name">group.send.user.tree</field>
<field name="model">group_send.user</field>
<field name="arch" type="xml">
<tree string="Group Send Users">
<field name="name"/>
<field name="is_email"/>
<field name="is_tg"/>
<field name="email"/>
<field name="tg"/>
</tree>
</field>
</record>
<!-- 用户搜索视图 -->
<record id="group_send_search_view" model="ir.ui.view">
<field name="name">group.send.user.search</field>
<field name="model">group_send.user</field>
<field name="arch" type="xml">
<search>
<field name="name"/>
<field name="email"/>
<field name="tg"/>
<group string="Group By">
<filter string="Email" name="email" context="{'group_by': 'email'}"/>
<filter string="TG" name="tg" context="{'group_by': 'tg'}"/>
</group>
</search>
</field>
</record>
<!-- 用户视图 -->
<record id="group_send_action" model="ir.actions.act_window">
<field name="name">Group Send Users</field>
<field name="res_model">group_send.user</field>
<field name="view_mode">tree,form</field>
</record>
<!-- menu -->
<menuitem
id="group_send_menu"
name="Group Send"
action="group_send_action"
groups="base.group_user"
sequence="100"/>
</odoo>
这里就遇到坑了, 主要是菜单出不来, 菜单最重要的声明是:
<menuitem
id="group_send_menu"
name="Group Send"
action="group_send_action"
groups="base.group_user"
sequence="100"/>
首先, 如果要在主菜单显示, 这么是不需要parent属性的, 其次, 必须要有groups属性.
但是这个时候菜单还出不来, 需要修改 ir.model.access.csv
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_group_send_user,group_send_user,model_group_send_user,base.group_user,1,1,1,1
打开调试模式
如果是在开发阶段, 最好打开odoo的调试模式, 使用快捷键ctrl+k
, 然后搜索debug
.
更新应用
打开apps应用列表页面, 更新应用. 更新了之后, 就有了Group Send了.
激活了以后点击菜单:
最终效果
点击Group Send以后, 就是新增的页面了.