美文网首页Odoo 10 LearnodooOdoo10学习
odoo V10开发文档(入门:建立网站)

odoo V10开发文档(入门:建立网站)

作者: XiaoHaiYang | 来源:发表于2017-04-07 17:02 被阅读1787次

    1.创建模块

    odoo提供scaffold命令用来快速创建模块:

    odoo scaffold <module name> <module directory>
    

    2.模块控制器

    打开module name文件夹,如mymodules/academy ,编辑controllers/controllers.py,在init.py进行导入

    # -*- coding: utf-8 -*-
    from odoo import http
    
    class Academy(http.Controller):
        @http.route('/academy/academy/', auth='public')
        def index(self, **kw):
            return "Hello, world"
    

    保存,重启odoo,此处如果模块文件夹不是默认,执行的时候需要指定addon目录(当前路径相对路径或绝对路径):

    odoo --addons-path addons,mymodules
    

    在开发者模式下更新应用列表,在设置-应用右上角搜索栏中搜索academy(需去掉应用过滤)安装
    ,安装完成后在浏览器输入 localhost:8069/academy/academy即可看到简单的hello word页面了

    3.自定义模板

    odoo自己开发了一套QWeb模板引擎,修改controller让它加载自定义模板:

    class Academy(http.Controller):
    @http.route('/academy/academy/', auth='public')
    def index(self, **kw):
        return http.request.render('academy.index', {
            'teachers': ["Diana Padilla", "Jody Caroll", "Lester Vaughn"],
        })
    

    然后编辑template.xml,此模板在manifest.py中配置到data中加载:

    <odoo>
    <data>
        <template id="index">
            <title>Academy</title>
            <t t-foreach="teachers" t-as="teacher">
              <p><t t-esc="teacher"/></p>
            </t>
        </template>
    </data>
    </odoo>
    

    此模板将teachers循环输出,重启odoo服务,找到academy点击升级,刷新localhost:8069/academy/academy页面会显示新的内容了

    4.数据保存

    • 模型定义

    编辑models/models.py

    class Teachers(models.Model):
    _name = 'academy.teachers'
    
    name = fields.Char()
    

    编辑security/ir.model.access.csv并加入manifest.py

    id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
    access_academy_teachers,access_academy_teachers,model_academy_teachers,,1,0,0,0
    

    此处即设置数据为对所有用户只读

    所有xml和csv文件都需要在manifest内进行引入,controller和module文件需要在init内进入导入

    • 测试数据,编辑demo.xml
    <odoo>
        <data>
            <record id="padilla" model="academy.teachers">
                <field name="name">Diana Padilla</field>
            </record>
            <record id="carroll" model="academy.teachers">
                <field name="name">Jody Carroll</field>
            </record>
            <record id="vaughn" model="academy.teachers">
                <field name="name">Lester Vaughn</field>
            </record>
        </data>
    </odoo>
    
    • 数据读取
      修改controllers.py
    class Academy(http.Controller):
        @http.route('/academy/academy/', auth='public')
        def index(self, **kw):
            Teachers = http.request.env['academy.teachers']
            return http.request.render('academy.index', {
                'teachers': Teachers.search([])
            })
    

    修改templates.xml

      <template id="index">
                <title>Academy</title>
                <t t-foreach="teachers" t-as="teacher">
                    <p><t t-esc="teacher.id"/> <t t-esc="teacher.name"/></p>
                </t>
            </template>
    

    重启odoo服务,升级academy(升级时会自动创建数据表,导入测试数据),重新打开localhost:8069/academy/academy 此时展现的数据就是数据库读取出来的了

    5.模块用于网站

    odoo提供了一个用于建立网站的模块,接下来测试academy用于网站中

    • 在manifest中将模块定义为website:
      'depends': ['website']
    • 在controller中指定为website:
    class Academy(http.Controller):
        @http.route('/academy/academy/', auth='public', website=True)
    
    • 在模板中添加website layout
    <template id="index">
                <t t-call="website.layout">
                    <t t-set="title">Academy</t>
                    <div class="oe_structure">
                        <div class="container">
                            <t t-foreach="teachers" t-as="teacher">
                                <p><t t-esc="teacher.id"/> <t t-esc="teacher.name"/></p>
                            </t>
                        </div>
                    </div>
                </t>
            </template>
    

    重启odoo并更新模块(加载template和manifest),重新进入academy页面,页面已经用了网站前端的布局

    6.链接和路由

    在controller中通过route()装饰器来处理请求,除了标准的URL之外,还可以接受参数型url,例:

     @http.route('/academy/<name>/', auth='public', website=True)
        def teacher(self, name):
            return '<h1>{}</h1>'.format(name)
            
      @http.route('/academy/<int:id>/', auth='public', website=True)
        def teacher_id(self, id):
            return '<h1>{} ({})</h1>'.format(id, type(id).__name__)
    
    

    重启odoo,浏览器进入academy/test和academy/1 所显示出来的页面是不一样的,odoo还支持使用数据模型做参数

    #controllers.py
    @http.route('/academy/<model("academy.teachers"):teacher>/', auth='public', website=True)
        def teacher(self, teacher):
            return http.request.render('academy.biography', {
                'person': teacher
            })
    
    #template.xml
    <template id="biography">
                <t t-call="website.layout">
                    <t t-set="title">Academy</t>
                    <div class="oe_structure"/>
                    <div class="oe_structure">
                        <div class="container">
                            <p><t t-esc="person.id"/> <t t-esc="person.name"/></p>
                        </div>
                    </div>
                    <div class="oe_structure"/>
                </t>
            </template>
    

    重启odoo并升级模块,输入academy/1 查看效果

    7.使模块在菜单栏可见

    默认情况下,新建的模块对用户来说是不可见的,需要创建一个菜单用户才能在页面中看到

    #view.xml
     <!-- 默认列表 -->
        <record id="action_academy_teachers" model="ir.actions.act_window">
        <field name="name">Academy teachers</field>
        <field name="res_model">academy.teachers</field>
      </record>
    
      <!-- 单条数据视图 -->
       <record id="academy_teacher_form" model="ir.ui.view">
        <field name="name">Academy teachers: form</field>
        <field name="model">academy.teachers</field>
        <field name="arch" type="xml">
          <form>
            <sheet>
              <label for="name"/> <field name="name"/>
              <label for="biography"/>
              <field name="biography"/>
            </sheet>
          </form>
        </field>
      </record>
    
      <!-- 菜单 -->
      <menuitem sequence="0" id="menu_academy" name="Academy"/>
      <menuitem id="menu_academy_content" parent="menu_academy"
                name="Academy Content"/>
      <menuitem id="menu_academy_content_teachers"
                parent="menu_academy_content"
                action="action_academy_teachers"/>
    
    

    重启odoo并更新模块,菜单会有academy入口了,默认会展现一个列表,点进去可编辑

    8.数据模型相互关联

    建立一个课程表模型,每个课程需要有一个老师,课程表就需要有一个老师ID的字段来进行关联,需要建立一个一对多的关系

    #models.py 添加
    class Teachers(models.Model):
        _name = 'academy.teachers'
    
        name = fields.Char()
        biography = fields.Html()
    
        course_ids = fields.One2many('academy.courses', 'teacher_id', string="Courses")
        
    class Courses(models.Model):
        _name = 'academy.courses'
    
        name = fields.Char()
        teacher_id = fields.Many2one('academy.teachers', string="Teacher")
    
    #ir.model.access.csv 添加
    access_academy_courses,access_academy_courses,model_academy_courses,,1,0,0,0
    
    #view.xml 更新
    <!-- 单条数据视图 -->
       <record id="academy_teacher_form" model="ir.ui.view">
        <field name="name">Academy teachers: form</field>
        <field name="model">academy.teachers</field>
        <field name="arch" type="xml">
          <form>
            <sheet>
              <label for="name"/> <field name="name"/>
    
              <label for="biography"/>
              <field name="biography"/>
    
              <field name="course_ids">
                <tree string="Courses" editable="bottom">
                  <field name="name"/>
                </tree>
              </field>
            </sheet>
          </form>
        </field>
      </record>
    <!-- 课程视图 -->
      <record id="action_academy_courses" model="ir.actions.act_window">
        <field name="name">Academy courses</field>
        <field name="res_model">academy.courses</field>
      </record>
      <record id="academy_course_search" model="ir.ui.view">
        <field name="name">Academy courses: search</field>
        <field name="model">academy.courses</field>
        <field name="arch" type="xml">
          <search>
            <field name="name"/>
            <field name="teacher_id"/>
          </search>
        </field>
      </record>
      <record id="academy_course_list" model="ir.ui.view">
        <field name="name">Academy courses: list</field>
        <field name="model">academy.courses</field>
        <field name="arch" type="xml">
          <tree string="Courses">
            <field name="name"/>
            <field name="teacher_id"/>
          </tree>
        </field>
      </record>
      <record id="academy_course_form" model="ir.ui.view">
        <field name="name">Academy courses: form</field>
        <field name="model">academy.courses</field>
        <field name="arch" type="xml">
          <form>
            <sheet>
              <label for="name"/>
              <field name="name"/>
              <label for="teacher_id"/>
              <field name="teacher_id"/>
            </sheet>
          </form>
        </field>
      </record>
    
      <!-- 菜单 -->
      <menuitem sequence="0" id="menu_academy" name="Academy"/>
      <menuitem id="menu_academy_content" parent="menu_academy"
                name="Academy Content"/>
      <menuitem id="menu_academy_content_courses"
                parent="menu_academy_content"
                action="action_academy_courses"/>
      <menuitem id="menu_academy_content_teachers"
                parent="menu_academy_content"
                action="action_academy_teachers"/>
    

    此时重启odoo并更新模块,进入academy,点击菜单的course,看看会有什么

    9.odoo聊天系统

    odoo自带聊天系统,只需要在models里引入mail.thread,添加messageid到表单中即可,下面为课程添加讨论功能

    #__manifest__.py 
    'depends': ['base','mail'],
    
    #models.py
    class Courses(models.Model):
        _name = 'academy.courses'
        _inherit = 'mail.thread'
    
        name = fields.Char()
        teacher_id = fields.Many2one('academy.teachers', string="Teacher")
    
    #views.xml
    <form>
        <sheet>
          <label for="name"/>
          <field name="name"/>
          <label for="teacher_id"/>
          <field name="teacher_id"/>
        </sheet>
        <div class="oe_chatter">
          <field name="message_follower_ids" widget="mail_followers"/>
          <field name="message_ids" widget="mail_thread"/>
        </div>
      </form>
    

    10.odoo交易功能

    odoo提供了website_sales的交易模块,只需要做简单的修改就可以实现商品发布(安装电子商务模块)

    #__manifest__.py
    'depends': ['website_sale'],
    
    'data': [
            'security/ir.model.access.csv',
            'views/views.xml',
            'views/templates.xml',
            'data/data.xml' #此处为新增
        ],
        
    #data/data.xml 新增
    <odoo>
    <data>
      <record model="product.public.category" id="category_courses">
        <field name="name">Courses</field>
        <field name="parent_id" ref=""/>
      </record>
    </data>
    </odoo>
    
    #demo.xml
    <record id="padilla" model="academy.teachers">
            <field name="name">老师1号</field>
    </record>
    <record id="course0" model="product.template">
        <field name="name">Course 0</field>
        <field name="teacher_id" ref="padilla"/>
        <field name="public_categ_ids" eval="[(4, ref('academy.category_courses'), False)]"/>
        <field name="website_published">True</field>
        <field name="list_price" type="float">0</field>
        <field name="type">service</field>
    </record>
    <record id="course1" model="product.template">
        <field name="name">Course 1</field>
        <field name="teacher_id" ref="padilla"/>
        <field name="public_categ_ids" eval="[(4, ref('academy.category_courses'), False)]"/>
        <field name="website_published">True</field>
        <field name="list_price" type="float">0</field>
        <field name="type">service</field>
    </record>
    <record id="course2" model="product.template">
        <field name="name">Course 2</field>
        <field name="teacher_id" ref="vaughn"/>
        <field name="public_categ_ids" eval="[(4, ref('academy.category_courses'), False)]"/>
        <field name="website_published">True</field>
        <field name="list_price" type="float">0</field>
        <field name="type">service</field>
    </record>
    
    #models.py
    class Teachers(models.Model):
        _name = 'academy.teachers'
    
        name = fields.Char()
        biography = fields.Html()
    
        course_ids = fields.One2many('product.template', 'teacher_id', string="Courses")
    
    class Courses(models.Model):
        _inherit = 'product.template'
    
        name = fields.Char()
        teacher_id = fields.Many2one('academy.teachers', string="Teacher")
        
    #ir.model.access.csv
    id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
    access_academy_teachers,access_academy_teachers,model_academy_teachers,,1,0,0,0
    
    #views.xml 去掉前面步骤创建的课程视图
    <!-- 单条数据视图 -->=
    <record id="academy_teacher_form" model="ir.ui.view">
    <field name="name">Academy teachers: form</field>
    <field name="model">academy.teachers</field>
    <field name="arch" type="xml">
      <form>
        <sheet>
          <label for="name"/> <field name="name"/>
    
          <label for="biography"/>
          <field name="biography"/>
    
          <field name="course_ids">
            <tree string="Courses" editable="bottom">
              <field name="name"/>
            </tree>
          </field>
        </sheet>
      </form>
    </field>
    </record>
    <menuitem sequence="0" id="menu_academy" name="Academy"/>
    <menuitem id="menu_academy_content" parent="menu_academy"
            name="Academy Content"/>
    <menuitem id="menu_academy_content_teachers"
            parent="menu_academy_content"
            />
    
    

    重启odoo,打开网站-商店,发现有添加的课程了


    内容发布自http://www.dingyii.cn,转载请注明出处

    相关文章

      网友评论

      • hoobyy:能支持sqlserver吗
        XiaoHaiYang:这个不支持sqlserver的,它是基于postgresql的,如果原有数据在sqlserver,可以想办法进行数据同步

      本文标题:odoo V10开发文档(入门:建立网站)

      本文链接:https://www.haomeiwen.com/subject/mdwhattx.html