跳至内容
菜单
此问题已终结
1 回复
8395 查看

I'm trying to set default values for a child field in the parent module.
Those default values have to be created in the database first.
However, my issue is to link them to the parent, since the parent record itself isn't yet created in the default method.

My Code in the parent module:

_columns = { ...
    'project_line_ids': fields.one2many('timesheet.project.line','timesheet_line_id','Project Lines'),
}

def _get_default_project_lines(self, cr, uid, context=None):
    emp_ids = self.pool.get('hr.employee').search(cr, uid, [('user_id', '=', uid)]);
    p_line = self.pool.get('timesheet.project.line');
    vals = p_line.default_get(cr, uid, ['timesheet_line_id', 'comment', 'used time'], context=None);
    ids = [];
    for p_id in self.pool.get('project.project').search(cr, uid, [('member_ids', 'in', emp_ids), ('active', '=', True)]):
        vals['project_id']=p_id;
        ids.append(p_line.create(cr, uid, vals, context=None)); 
    return ids;

_defaults = { ...
    'project_line_ids': _get_default_project_lines,

Snippet from the child module:

_columns = { ...
    'timesheet_line_id': fields.many2one('timesheet.line', 'Timesheet Line', ondelete='cascade'),
}

To explain: The parent is a line in a timesheet. And i want each line to have several child records of project.line The issue is not that the children aren't created. But they are not linked to the parent correctly. I've tried to use active_id in the context of the field in the view to set the default value for the timesheet_line_id, but that fails..

View:

<field name="project_line_ids" context="{'default_timesheet_line_id': active_id, 'tree_view_ref': 'timesheet.view_timesheet_project_line_tree'}"></field>


Any advice?
Is there a way to get the id of the new record during creation process?

形象
丢弃
最佳答案

I think your way is bad design. In my eyes there are two other ways.

1) Update the write() method. But this should work on saving but in general generating other objects makes it bad handle in the long run.

2) The clean way, is not patching the object. Instead make a wizard that creates a pre-define default object. Than a user can trigger this wizard if he want, but if the object was create in a flow of other prozess or module it will not be changed.

形象
丢弃
编写者

Thanks for your reply. I see your point and will try to use a wizard. But i dont quite get how to handle this yet. What do you mean by 'pre-defined default object' ? Regards

编写者

So instead of creating the project_lines directly in the database table, i will use a wizard to create them?

Yes, like transform a sale_order as a invoice or make hr_timesheet to a invoice

相关帖文 回复 查看 活动
0
3月 15
3642
0
4月 24
2074
2
6月 23
3138
2
1月 23
2561
1
8月 22
5572