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.inheritEither 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>
Hi Gregoire, first replace name _column to _columns and check it.
Working! Thanks.