简体   繁体   中英

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

在此输入图像描述

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. Something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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