This question has been flagged

Dear all,

I have field tag_ids configured on the project.project model.

I then want to create new tasks and want to copy the tag_ids from project.project to the respective new task as default values.

I tried many different ways but I don't get it to work.


Could you please help me out.

class TaskTemplating(models.Model):
_inherit = 'project.project'
tags_ids = fields.Many2many('project.tags',
string="Stichwörter")

class DescriptionTemplate(models.Model):
_inherit = 'project.task'

api.model
def default_get(self, vals):
    task = super(DescriptionTemplate, self).default_get(vals)
task.update({
'tag_ids' : self.env['project.task'].project_id._context.get('tag_ids'),
})
    return task

I would truly appreciate your help

Avatar
Discard
Best Answer

ORM methods (Create/update) : https://www.odoo.com/documentation/13.0/reference/orm.html#common-orm-methods

One2many and Many2many use a special “commands” format to manipulate the set of records stored in/associated with the field.

This format is a list of triplets executed sequentially, where each triplet is a command to execute on the set of records. Not all commands apply in all situations. Possible commands are:

(0, 0, values) adds a new record created from the provided value dict.

(1, id, values) updates an existing record of id id with the values in values. Can not be used in create().

(2, id, 0) removes the record of id id from the set, then deletes it (from the database). Can not be used in create().

(3, id, 0) removes the record of id id from the set, but does not delete it. Can not be used in create().

(4, id, 0) adds an existing record of id id to the set.

(5, 0, 0) removes all records from the set, equivalent to using the command 3 on every record explicitly. Can not be used in create().

(6, 0, ids) replaces all existing records in the set by the ids list, equivalent to using the command 5 followed by a command 4 for each id in ids.


Example:

task.update({'tag_ids': [( 6, 0, ids_list)]})


Avatar
Discard
Author

How can I retrieve the ids_list from project.project?

Here example:

project = self.env['project.project'].browse(project_id)

project_tag_ids = project.tag_ids

ids_list = project_tag_ids.ids or ids_list = [ project_tag_id.id for project_tag_id in project_tag_ids]

in project.task class

tag_ids = fields.Many2many('project.tags', string='Tags', default=_get_default_tag_ids)

def _get_default_tag_ids(self):

""" Gives default tags_ids """

project_id = self.env.context.get('default_project_id')

if project_id:

project = self.env['project.project'].search([('id', '=', project_id)

return self.tag_ids= [( 6, 0, project.tags_ids)]

else:

return False

Author

Dear Gleb and paidy kumar.

You are great. I got it working with your examples. I'm really happy, cause it's been occupying me for a week now.

I implemented it like this now together with 3 more fields:

@api.model

def default_get(self, vals):

task = super(DescriptionTemplate, self).default_get(vals)

project_id = self.env.context.get('default_project_id')

project = self.env['project.project'].browse(project_id)

project_tag_ids = project.tag_ids

ids_list = project_tag_ids.ids

task.update({

'color_gantt' : project.color_gantt,

'on_gantt' : project.on_gantt,

'tag_ids' : [(6,0,ids_list)]

})

return task