Skip to Content
Menu
This question has been flagged
2 Replies
9384 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")

Avatar
Discard
Best Answer

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.

Avatar
Discard
Best Answer

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

Avatar
Discard