Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
8400 Vistas

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?

Avatar
Descartar
Mejor respuesta

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.

Avatar
Descartar
Autor

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

Autor

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

Publicaciones relacionadas Respuestas Vistas Actividad
0
mar 15
3642
0
abr 24
2075
2
jun 23
3138
2
ene 23
2562
1
ago 22
5578