Odoo Help

0

Model with many2many relation to his self

By
Michael Berg
on 6/25/15, 4:09 AM 1,184 views

Hello

I want to create a model Category who can has many SubCategories and can be a SubCategory of more than one Category.

How I am able to do this in Odoo v8.0


This is the way I would do it. But there raise an error that i'm not able to create a relation to the same model.

class ebay_categories(models.Model):

_name = "famberg.categories"

_rec_name = 'CategoryName'

_sql_constraints = {('unique_CategoryID', 'unique(CategoryID)', 'The CategoryID needs to be unique!')}


CategoryID = fields.Char(size=10, string="Ebay Category ID")

CategoryName = fields.Char(size=30, string="Ebay Category Name")

CategoryParentIDs = fields.many2many(comodel_name="famberg..categories",inverse_name="Category", string="Parrent Categorys")

0
Michael Mugge
On 4/11/17, 2:24 PM

The way I have observed it done was by looking at how the categories for partner records work.  The res_partner_category table stores these. Since a category can belong to another category, it's basically the same thing you are going for. Since this question is old, I am going to provide a link to the reference code for Odoo 9:

https://github.com/odoo/odoo/blob/fc2e80cb4bcc450762c7ac5cb82a3e2d88062b38/odoo/addons/base/res/res_partner.py#L77

Note that the defined relationships in the referenced code do not use Many2many, but instead, Many2one and One2many which makes more sense if your categorizations have a hierarchy.

0
Bole
On 6/29/15, 8:20 AM

When creating m2m relation, both related models must have a m2m field... in your case, it is one model, but is still need to have BOTH m2m fields... 
so i guess you should define :

CategoryParentIDs = fields.Many2many(comodel_name='famberg.categories'...
CategoryChildIDs = fields.Many2many(comodel_name='famberg.categories'...

in order to have proper many2many relation on the same model


hope it helps

About This Community

This platform 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.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)

Stats

Asked: 6/25/15, 4:09 AM
Seen: 1184 times
Last updated: 4/11/17, 2:24 PM