跳至内容
菜单
此问题已终结

I'm trying to develop simple library app with Odoo but I get an error with manytomany relationship.

Here are classes:

class Book(models.Model):
    _name = "library.book"
    publisher_id = fields.Many2one(comodel_name="library.book.partner", string="Publisher")
    author_ids = fields.Many2many(comodel_name="library.book.partner", relation='library_book_profile_partner_authors', column1='book_id', column2='partner_id', string="Authors")
class Partner(models.Model):
    _name = "library.book.partner"
    _inherit = "res.partner"
    published_book_ids = fields.One2many("library.book", "publisher_id", string="Published Books")
    book_ids = fields.Many2many("library.book", 'partner_books', 'book_id', 'partner_id', string="Authored Books")

This is the error I always get when installing the app

TypeError: Many2many fields library.book.partner.channel_ids and res.partner.channel_ids use the same table and columns 

Someone can help to solve this please ?

形象
丢弃
最佳答案

You are creating a new object "library.book.partner" by inheriting "res.partner". That is all fields of res.partner will be available in library.book.partner apart from the new fields defined. Now there is a field called channel_ids is defined in res.partner in the mail module, this is a many2many field and a copy of this field will be there in library.book.partner, and both use the same table and columns. This is not allowed and creates the error, so you have to define a new channel_ids field with a different table name in your model.

Here is the field definition in res.partner model:-

    channel_ids = fields.Many2many('mail.channel', 'mail_channel_partner', 'partner_id', 'channel_id', string='Channels', copy=False)

One other issue is that for many2may fields author_ids and book_ids you are using different relation[table name]. Instead, use the same table name and column1, column2 inversed.


class Book(models.Model):
_name = "library.book"
publisher_id = fields.Many2one(comodel_name="library.book.partner", string="Publisher")
author_ids = fields.Many2many(comodel_name="library.book.partner", relation='library_book_profile_partner_authors', column1='book_id', column2='partner_id', string="Authors")

class Partner(models.Model):
_name = "library.book.partner"
_inherit = "res.partner"
published_book_ids = fields.One2many("library.book", "publisher_id", string="Published Books")
book_ids = fields.Many2many("library.book", 'library_book_profile_partner_authors', 'partner_id', 'book_id', string="Authored Books")

channel_ids = fields.Many2many('mail.channel', 'mail_channel_partner_book', 'partner_id', 'channel_id', string='Channels', copy=False)


形象
丢弃

Adding Channel_ids not work in my odoo 15

Please include more details of your error

相关帖文 回复 查看 活动
1
10月 22
3086
1
9月 22
3177
1
8月 22
2767
1
3月 24
1761
1
12月 23
2538