Odoo Help

Welcome!

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.

0

Creating model data with many2many field doesn't work

By
Tobias Wetzel
on 6/23/15, 4:46 AM 1,178 views

Hi,

I am trying to create a model entry in my controller, which has a many2many field. The creating works but the values I added to the many2many field are not there. What is wrong?

Model definition:

class Paper(models.Model):
_name = 'paper_submission.paper'

title = fields.Char(string='Title', required='True')
author_ids = fields.Many2many('paper_submission.author', string='Authors', relation="paper_submission_paper_author_relation")

references model:

class Author(models.Model):
_name = 'paper_submission.author'

first_name = fields.Char()
last_name = fields.Char()

Code for creating the model entry:

 orm_paper = registry.get('paper_submission.paper')

paper_info = {"title: title, author_ids": author_ids}
paper_id = orm_paper.create(cr, SUPERUSER_ID, paper_info, context=context)

where author_ids is a list of ids, like [1,2]


SOLUTION: orm_paper.create({'title': title, 'author_ids': [(6, 0, author_ids)]}) using: (6, _, ids) triplet from the Odoo docs: "Values marked as _ in the list above are ignored and can be anything, generally 0 or False."

Tobias Wetzel
on 6/24/15, 3:46 AM
2
Habib
On 6/23/15, 9:50 PM

You need to use tuples to write to many2many records:

In your case if the authors don't exist, and you want to create them, you would use:

orm_paper.create({'title': title, 'author_ids': [(0, 0, {'first_name': first_name, 'last_name': last_name})]

if the author does exist you would use:

orm_paper.create({'title': title, 'author_ids': [(4, author_id)]}) 


The following can be found at /openerp/models.py, and describes how to use the appropriate "triplet" 

:class:`~openerp.fields.One2many` and
:class:`~openerp.fields.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, _, 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 :meth:`~.create`. ``(2, id, _)`` removes the record of id ``id`` from the set, then deletes it (from the database). Can not be used in :meth:`~.create`. ``(3, id, _)`` removes the record of id ``id`` from the set, but does not delete it. Can not be used on :class:`~openerp.fields.One2many`. Can not be used in :meth:`~.create`. ``(4, id, _)`` adds an existing record of id ``id`` to the set. Can not be used on :class:`~openerp.fields.One2many`. ``(5, _, _)`` removes all records from the set, equivalent to using the command ``3`` on every record explicitly. Can not be used on :class:`~openerp.fields.One2many`. Can not be used in :meth:`~.create`. ``(6, _, 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``. Can not be used on :class:`~openerp.fields.One2many`.

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)

Stats

Asked: 6/23/15, 4:46 AM
Seen: 1178 times
Last updated: 6/24/15, 3:46 AM