Odoo Help


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.


Default values for child field

René Schuster
on 12/3/13, 6:38 AM 2,229 views

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)]):
        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..


<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?

Torsten Francke
On 12/3/13, 10:09 AM

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

René Schuster
on 12/5/13, 5:14 AM

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

René Schuster
on 12/6/13, 4:17 AM

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

initOS GmbH, Torsten Francke
on 12/6/13, 8:55 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)


Asked: 12/3/13, 6:38 AM
Seen: 2229 times
Last updated: 3/16/15, 8:10 AM