简体   繁体   English

在Odoo中继承自定义模块

[英]Inheritance of customized module in Odoo

I'm trying to create a module ( project_photo ) for uploading photos related to customer projects with a button next to "Documents": 我正在尝试创建一个模块( project_photo ),用于通过“Documents”旁边的按钮上传与客户项目相关的照片:

在此输入图像描述

Since I need the photo count for each project, I'm inheriting the project.project module so I can add the photo_count function field. 由于我需要每个项目的照片计数,我继承了project.project模块,因此我可以添加photo_count函数字段。 Something like this: 像这样的东西:

project_photo.py project_photo.py

# -*- encoding: utf-8 -*-
from openerp.osv import fields, osv
from openerp.tools.translate import _


class my_project(osv.osv):
    def _get_attached_photos(self, cr, uid, ids, field_name, arg, context):
        res = {}
        project_photos = self.pool.get('project.photo')
        for id in ids:
            photo = project_photos.search(cr, uid, [('project_id', '=', id)], context=context, count=True)
            res[id] = photo or 0
        return res

    def photo_tree_view(self, cr, uid, ids, context):
        photo_ids = self.pool.get('project.photo').search(cr, uid, [('project_id', 'in', ids)])
        domain = [
             '|',
             '&', ('res_model', '=', 'project.project'), ('res_id', 'in', ids),
             '&', ('res_model', '=', 'project.photo'), ('res_id', 'in', photo_ids)
        ]
        res_id = ids and ids[0] or False
        return {
            'name': _('Photos'),
            'domain': domain,
            'res_model': 'ir.attachment',
            'type': 'ir.actions.act_window',
            'view_id': False,
            'view_mode': 'kanban,tree,form',
            'view_type': 'form',
            'limit': 80,
            'context': "{'default_res_model': '%s','default_res_id': %d}" % (self._name, res_id)
        }

    _name = 'project.project'
    _inherit = 'project.project'
    _columns = {
        'photo_count': fields.function(
            _get_attached_photos, string='Number of photos attached', type='integer'
        ),
    }

my_project()


class project_photo(osv.osv):
    _name = 'project.photo'
    _columns = {
        'project_id': fields.many2one(
            'project.project',
            'Project',
            ondelete='cascade'
        ),
        'photo': fields.binary('Photo'),
    }

project_photo()

My view is inheriting project.edit_project and I'm placing my button after the doc_count button: 我的观点是继承project.edit_project ,我在doc_count按钮后放置了我的按钮:

project_photo.xml project_photo.xml

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <record id="project_photos" model="ir.ui.view">
            <field name="name">project.project.form</field>
            <field name="model">project.project</field>
            <field name="inherit_id" ref="project.edit_project" />
            <field name="arch" type="xml">
                <field name="doc_count" position="after">
                     <button  class="oe_inline oe_stat_button" name="photo_tree_view" type="object" icon="fa-picture-o">
                        <field string="Photos" name="photo_count" widget="statinfo" />
                    </button>
                </field>
            </field>
        </record>
    </data>
</openerp>

I'm getting this error when trying to install the module: 我在尝试安装模块时收到此错误:

...
ParseError: "ValidateError
Field(s) `arch` failed against a constraint: Invalid view definition

Error details:
Field `photo_count` does not exist
...

You should use button name for positioning rather than field name=doc_count. 您应该使用按钮名称进行定位,而不是使用字段名称= doc_count。 Try out the following code: 试试下面的代码:

<button name='attachment_tree_view' position='after'>
<button  class="oe_inline oe_stat_button" name="photo_tree_view" type="object" icon="fa-picture-o">
    <field string="Photos" name="photo_count" widget="statinfo" />
</button>

I hope this helps you out. 我希望这能够帮到你。

Thanks And Regards 谢谢并恭祝安康

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM