Custom module inheritance in Odoo

I am trying to create a module ( project_photo

) to upload photos related to client projects using the button next to Documents:

enter image description here

Since I need a photo counter for each project, I inherit the module project.project

, so I can add a function field photo_count

. Something like that:

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 inherits project.edit_project

and I place my button after the button 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>

      

I am getting this error when trying to install a module:

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

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

      

+3


source to share


1 answer


You should use the button name for positioning, not the field name = doc_count. Try using 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>

      



Hope this helps you.

Thanks and Regards

+1


source







All Articles