0

I assign for each project many tasks into task view form by using many2one field related project model. Then i create one2many field into project model to retrieve tasks directly into project View form, and into the code i'm looping from all the one2many fields for all tasks and append value into the one one2many field for displaying all tasks,the project_id_for_project is many2one field inside project model used to give the abaility of when i select one project it give all the attached tasks i hope you get my idea so i created the two model like below:

class project(models.Model):
    _name = 'project.project'
    _description = 'project.project'

    project_id_for_project = fields.Many2one('project.project' )
    Project_task_aff = fields.One2many('tasks.tasks','task_aff')

    @api.onchange('project_id_for_project')
    def getone2manyproject(self):
        for rec in self:
            lines = []
            for line in rec.project_id_for_project.Project_task_aff :
                val = {
                    # 'task_aff' : line.id ,
                    'task_actor' : line.task_actor,
                    'name' : line.name,
                    'DATE_tsk' : line.DATE_tsk,
                    'Description' : line.Description,
                }

                lines.append((0,0, val))

            rec.Project_task_aff = lines

and the task model :

class tasks(models.Model):
    _name = 'tasks.tasks'
    _description = 'tasks.tasks'
    _inherit = ['mail.thread', 'mail.activity.mixin']

    task_actor = fields.Many2one('res.users', string='Chauffeur')

    task_aff = fields.Many2one('project.project')
    name = fields.Char()
    DATE_tsk = fields.Date()
    Description = fields.Char()
    project_id = fields.Many2one('project.project')

the code give update the one2many field Project_task_aff but it dont give all the taks from task module: it mean that when i go into task model view, and create 5 entries related to a project, but when the onchange methode based of project_id_for_project field didn't give the 5 stored task but append just whose are stored into the onetomany field into the project view ?

Sadiki Ayoub
  • 101
  • 1
  • 10
  • Does this answer your question? [add many2many and one2many record through python code -Odoo 12 / Python 3.7](https://stackoverflow.com/questions/65086490/add-many2many-and-one2many-record-through-python-code-odoo-12-python-3-7) – Adam Strauss Jan 30 '23 at 07:05

3 Answers3

0

You need to apply the relational field rules to update the value.

Rules:

(0, 0, { values }) link to a new record that needs to be created with the given values dictionary

(1, ID, { values }) update the linked record with id = ID (write values on it)

(2, ID) remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)

(3, ID) cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)

(4, ID) link to existing record with id = ID (adds a relationship)

(5) unlink all (like using (3,ID) for all linked records)

(6, 0, [IDs]) replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)

        rec.Project_task_aff = [(6, 0, lines)]

I think this URL help with this problem and debugging.

https://raise360.odoocommunity.net/slides/slide/how-to-update-one2many-field-from-onchange-of-field-124

0

Try this way.

@api.onchange('project_id_for_project')
def getone2manyproject(self):
    for rec in self:
        tasks = self.env['tasks.tasks'].search([('task_aff', '=', rec.id)])

        rec.Project_task_aff = [(6, 0, tasks.ids)]

Edit:

What is the purpose of project_id_for_project field in project's model for child parent relationship?

Adam Strauss
  • 1,889
  • 2
  • 15
  • 45
  • project_id_for_project is one2many field in that give the abaility of when i select one project it give all the attached tasks. What i want : I assign for each project many tasks into task view form by using many2one field related project model. Then I create one2many field into project model to retrieve tasks directly into project View form, and into the code I'm looping from all the one2many fields for all tasks and append value into the one one2many field for displaying all tasks, i hope you get my idea – Sadiki Ayoub Jan 30 '23 at 07:48
  • I think use 3 models one for the view other one is project.project and last one will be tasks.tasks. In first model create two fields project_id as many2one and other is task_ids as many2many. Then use `onchange` on project_id. – Adam Strauss Jan 30 '23 at 08:09
  • Your idea seems the solution but i wish i can have an exemple to do the same @Adam Strauss – Sadiki Ayoub Jan 31 '23 at 09:34
0

May be this will help, I just retrieve the first line browse loop self, and replace the tuple (0, 0, val) with (6, 0, val) in last line

@api.onchange('project_id_for_project')
def getone2manyproject(self):
    lines = []
    for line in self.project_id_for_project.Project_task_aff :
        val = {
            # 'task_aff' : line.id ,
            'task_actor' : line.task_actor.id,
            'name' : line.name,
            'DATE_tsk' : line.DATE_tsk,
            'Description' : line.Description,
        }

        lines.append((6, 0, val))

    self.Project_task_aff = lines
khelili miliana
  • 3,730
  • 2
  • 15
  • 28
  • When i set (6, 0, val), It show the folowing error : psycopg2.DataError: invalid input syntax for type integer: "task_actor" LINE 1: ...te" FROM "tasks_tasks" WHERE "tasks_tasks".id IN ('task_acto. if i put (0, 0, val) that give the same thing as previeous version of code, thanks in advance for your feedback – Sadiki Ayoub Jan 30 '23 at 15:12
  • Steel give me error : raise CacheMiss(record, field) odoo.exceptions.CacheMiss: ("tasks.tasks('task_actor',).task_actor", None) During handling of the above exception, another exception occurred: ..................... psycopg2.DataError: invalid input syntax for type integer: "task_actor" LINE 1: ...te" FROM "tasks_tasks" WHERE "tasks_tasks".id IN ('task_acto... – Sadiki Ayoub Jan 31 '23 at 08:35