gpt4 book ai didi

python - Odoo中自定义模块的继承

转载 作者:数据小太阳 更新时间:2023-10-29 02:40:29 25 4
gpt4 key购买 nike

我正在尝试创建一个模块 (project_photo),用于使用“文档”旁边的按钮上传与客户项目相关的照片:

enter image description here

因为我需要每个项目的照片数,所以我继承了 project.project 模块,所以我可以添加 photo_count 函数字段。像这样:

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

我的 View 继承了 project.edit_project 并且我将我的按钮放在 doc_count 按钮之后:

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>

我在尝试安装模块时遇到此错误:

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

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

最佳答案

您应该使用按钮名称进行定位,而不是字段名称=doc_count。试试下面的代码:

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

希望对您有所帮助。

感谢和问候

关于python - Odoo中自定义模块的继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26135274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com