Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

I try to inherit function and it fails odoo8

By
wizardz
on 6/21/16, 10:00 AM 390 views

I try to inherit this button in hr.timesheet.sheet.form:

<button  class="oe_inline oe_stat_button" name="attachment_tree_view"  type="object" icon="fa-files-o">
<field string="Documents" name="doc_count" widget="statinfo"/>
</button>

On my python I have this:

class project(osv.osv):
_inherit = "project.project"
def test(self, cr, uid, vals, context=None):
return super(project.project, self)._get_attached_docs(cr, uid, vals, context=context)
_columns = {
'doc_count': fields.function(
test, string="Number of documents attached", type='integer'
)
}

Now I get all time an error. What am I making wrong with the import of the function get_attached_docs ? How can I import this simple function to my field? and then put it on the button?


I want this 2 buttons from project.project on the hr.timesheet.sheet form with the same links behind that in project:



1
Juan Vicente Pascual
On 6/22/16, 6:40 AM

In your .py file you have to define the function that is called by the button "attachment_tree_view"

EDITED:

class project(osv.osv):
 _inherit = "project.project"

 def test(self, cr, uid, vals, context=None):
return super(project.project, self)._get_attached_docs(cr, uid, vals, context=context)

def attachment_tree_view(self,cr,uid,ids,context):
tasks_ids = self.pool.get(project.task).search(cr,uid,[('project_id','in',ids)])
domain = ['|','&',('res_model','=','project.project'),('res_id','in', ids),
'&',('res_model','=','project.project'),('res_id','in', task_ids)]
res_id = ids and ids[0] or False
return {
'name':_('Attachments'),
'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)
}
 _columns = {
 'doc_count': fields.function(test, string="Number of documents attached",type='integer')
 }

...


With this you inherit original function, now just modify as you need (remember to set super if you modify before or after the original functions returns the values).


Kind regards.

How can I inherit this function from project.project - project.py to my module ?

wizardz
on 6/23/16, 2:39 AM

I've modified my answer. see if this fits your needs.

Juan Vicente Pascual
on 6/23/16, 3:13 AM

How Can I display that in the hr.timesheet.sheet view? I want the button there, not in project.project. :)

wizardz
on 6/23/16, 5:19 AM
0
wizardz
On 6/23/16, 4:09 AM

I try to make this 2 buttons on the hr.timesheet.sheet view. 

My hr_timesheet.py looks like this:

# -*- coding: utf-8 -*- import openerp from openerp.osv import fields, osv

class hr_timesheet_sheet(osv.osv): _inherit = ["hr_timesheet_sheet.sheet"] _columns = { 'testfield1': fields.char(''), 'project_field': fields.many2one('project.project', 'name'), }

class project(osv.osv): _inherit = "project.project" def test(self, cr, uid, vals, context=None): return super(project.project, self)._get_attached_docs(cr, uid, vals, context=context) def attachment_tree_view(self, cr, uid, ids, context): task_ids = self.pool.get('project.task').search(cr, uid, [('project_id', 'in', ids)]) domain = [ '|', '&', ('res_model', '=', 'project.project'), ('res_id', 'in', ids), '&', ('res_model', '=', 'project.task'), ('res_id', 'in', task_ids)] res_id = ids and ids[0] or False return { 'name': _('Attachments'), '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) }

_columns = { 'doc_count': fields.function(test, string="Number of documents attached",type='integer') }

My XML file like this: hr_timesheet_view_test.xml:

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>         <record id="hr_timesheet_sheet_tree_test_buttons" model="ir.ui.view"> <field name="name">hr.test.button.tree</field> <field name="model">hr_timesheet_sheet.sheet</field> <field name="inherit_id" ref="hr_timesheet_sheet.hr_timesheet_sheet_form"/> <!-- "hr_presence.hr_timesheet_sheet_form_presence" --> <field name="arch" type="xml"> <xpath expr="//notebook" position="before"> <button class="oe_inline oe_stat_button" name="attachment_tree_view" type="object" icon="fa-files-o"> <field string="Documents" name="doc_count" widget="statinfo"/>                        </button>                     </xpath>             </field>         </record>     </data> </openerp>


This is not working , I get the error that the field : doc_count does not exist.

in your py file, you define field doc_count for model 'project.project'

in ypur view you have set model hr_timesheet_sheet.sheet

Kind regards.

Juan Vicente Pascual
on 6/23/16, 4:28 AM

now when I try to install that I get this error: attendance_count does not exist.

I don't have a field like that.

If I change the model in view to project.project it give me this error. I want this button in the timesheet view.

wizardz
on 6/23/16, 4:37 AM

in py.file remove 'project.project' inherited class and define doc_count and the functions in hr_timesheet_sheet class.

the modify functions to match your requeriments.

kind regards.

Juan Vicente Pascual
on 6/23/16, 5:25 AM

I have made that but it's not working . I don't understand how I can use this buttons in an other model view.

wizardz
on 6/23/16, 5:36 AM

you have to redefine, the views and py files in your model.

Juan Vicente Pascual
on 6/23/16, 6:51 AM

make the hr_timesheet.py to project.py ?

and make the hr_timesheet_view_test.xml to project_view.xml?

wizardz
on 6/23/16, 7:25 AM

please can you give me the right example that this works...

wizardz
on 6/23/16, 8:17 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 6/21/16, 10:00 AM
Seen: 390 times
Last updated: 6/23/16, 4:09 AM