This question has been flagged
1 Reply
4180 Views

I'm trying to add a new field "End date" in project.task.work that would be related to 'date' (start date) and 'duration'. That seems to be ok. But when I'm customizing the view project.view_task_form2 to add my new field end_date, I got a error of type ViewError:

Can't find field 'end_date' in the following view parts composing the view of object model 'project.task':
 * project.task.form.inherit

Either you wrongly customized this view, or some modules bringing those views are not compatible with your current data model

Here is the code:

class CustomProjectWork(osv.osv):
    _name = "project.task.work"
    _inherit = "project.task.work"

    def _determin_end_date(self, cr, uid, ids, field, arg, context=None):
        result = {}
        for projectwork in self.browse(cr, uid, ids, context=context):
            if projectwork.date and projectwork.hours:
                # Get a datetime object from projectwork.date
                start_date = datetime.strptime(projectwork.date, "%Y-%m-%d %H:%M:%S")
                # Get a timedelta object from projectwork.duration
                duration = timedelta(days=(projectwork.duration - 1))
                # Calculate the end time
                end_date = start_date + duration
                # Render in format YYYY-MM-DD
                result[projectwork.id] = end_date.strftime("%Y-%m-%d %H:%M:%S")
            else:
                result[projectwork.id] = projectwork.date
                return result

    def _set_end_date(self, cr, uid, id, field, value, arg, context=None):
        projectwork = self.browse(cr, uid, id, context=context)
        if projectwork.date and value:
            start_date = datetime.strptime(projectwork.date, "%Y-%m-%d %H:%M:%S")
            end_date = datetime.strptime(value[:20], "%Y-%m-%d %H:%M:%S")
            duration = end_date - start_date
            self.write(cr, uid, id, {'duration': (duration.days + 1)}, context=context)

    _column = {
        'end_date': fields.function(_determin_end_date, fnct_inv=_set_end_date, type='datetime', string="End Date", store=True)
    }

The view:

<record id="view_task_form2_inherit" model="ir.ui.view">
            <field name="name">project.task.form.inherit</field>
            <field name="model">project.task</field>
            <field name="inherit_id" ref="project.view_task_form2" />
            <field name="type">form</field>
            <field name="arch" type="xml">
                <field name="work_ids" position="replace">
                    <field colspan="4" name="work_ids" nolabel="1" attrs="{'readonly':[('state','in',['draft','done'])]}">
                        <tree string="Task Work" editable="top">
                            <field name="date" />
                            <field name="end_date" />
                            <field name="user_id" />
                            <field name="hours" widget="float_time" />
                            <field name="name" />
                        </tree>
                    </field>
                </field>
            </field>
</record>

Avatar
Discard

Hi Gregoire, first replace name _column to _columns and check it.

Author

Working! Thanks.

Best Answer

Hello,

The reason, why you getting this error is you have not restarted Odoo server or not updated your module.

Please restart your Odoo server and update the module in which you have written the code. Once you do this, in model class "end_date" will able to register and then in View there will be no problem.

And yes, Did you forgot to add class file in your __init__.py file ? I am sure you have forgot.

Please check everything and back to me. This is small problem and generally it comes when you are beginner. 

Avatar
Discard
Author

I have this message when I'm starting my server with an update of my module! __init__.py file contains the proper import. __openerp__.py is declaring the view in 'data' and make a dependency to 'project' into 'depends' entry.

Author

Also, there is no error if I comment the end_date field tag.

Oh my god !!! Please write your column under "Columns" not in "Column" Do change as follows, _columns = { 'end_date': fields.function(_determin_end_date, fnct_inv=_set_end_date, type='datetime', string="End Date", store=True) } (facepalm)

Yes, Please define your column under "_columns" , not in "_column".

Author

Ah indeed... Thanks guys.