Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Odoo one2many view

Tags:

python

xml

odoo

I am beginner in Odoo and python, and I want to create a simple module, which should insert some categories for a person (student, employee,...), and then insert persons with field category. My question is, how to create p_c_view.xml. I tried to adjust some xmls examples that I found, but had no success. Here is my p_c.py file. Thanks

 class p_c_person(osv.osv):
        _name = "p_c.person"
        _description = "Person"
        _columns = {
            'name': fields.char('Persone', size=128, required=True),
            'categories': fields.one2many('p_c.category', 'category_id', 'Categories'
        }

    p_c_person()

    class p_c_category(osv.osv):
        _name = "p_c.category"
        _description = "Category"
        _columns = {
            'name': fields.char('Category', size=128, required=True),
            'property_id': fields.many2one('p_c.person', 'Person Name', select=True),
        }

    p_c_category()
like image 485
Urosh Avatar asked Jun 16 '26 23:06

Urosh


2 Answers

Many2one

Store a relation against a co-model:

Specific options:

  • comodel_name: name of the opposite model
  • delegate: set it to True to make fields of the target model accessible from the current model (corresponds to _inherits)

Example:

'property_id': fields.many2one('p_c.person', 'Person Name', select=True),

One2many

Store a relation against many rows of co-model:

Specific options:

  • comodel_name: name of the opposite model
  • inverse_name: relational column of the opposite model

Example:

'categories': fields.one2many('p_c.category', 'property_id', 'Categories'),

Refer Odoo documentation

One2many field only created for relational many2one field of that model, it gives you freedom to travelling bi-directional. Odoo engine manage integrity while you create record in reference model in which you have define many2one field then you will see the effect in one2many field as well.

like image 76
Emipro Technologies Pvt. Ltd. Avatar answered Jun 19 '26 12:06

Emipro Technologies Pvt. Ltd.


one2many field takes responding many2one field with that object.

try with this code

class p_c_person(osv.osv):
    _name = "p_c.person"
    _description = "Person"
    _columns = {
        'name': fields.char('Persone', size=128, required=True),
        'categories': fields.one2many('p_c.category', 'property_id', 'Categories'
    }

p_c_person()

class p_c_category(osv.osv):
    _name = "p_c.category"
    _description = "Category"
    _columns = {
        'name': fields.char('Category', size=128, required=True),
        'property_id': fields.many2one('p_c.person', 'Person Name', select=True),
    }

p_c_category()

For more details you may visit odoo documentation.

like image 45
Bhavesh Odedra Avatar answered Jun 19 '26 14:06

Bhavesh Odedra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!