Trinh Ngoc Hung
Trinh Ngoc Hung

Reputation: 23

How to use "related" in odoo

I have class:

    class test(models.Models):
       _name = "student"
       .....
       gender = fields.Selection([
            ('m', 'Male'),
            ('f', 'Female'),
            ('o', 'Other')

and I have class another one:

class get_Value(models.Models):
     _name = "school"

     student_id = fields.Many2one("module.test", string="Student")
     gender = fields.Char(string="Gender", related="student_id.gender")

And odoo false.

Upvotes: 1

Views: 2166

Answers (2)

Kerrim
Kerrim

Reputation: 523

your code should look like this:

Student:

class Student(models.Models):
    _name = "modulename.student"
    
    gender = fields.Selection([
        ('m', 'Male'),
        ('f', 'Female'),
        ('o', 'Other')
    ])

School:

class School(models.Models):
     _name = "modulename.school"

     student_id = fields.Many2one("modulename.student", string="Student")
     gender = fields.Selection(string="Gender", related="student_id.gender")
  1. It is better to have class names that make sense. (e.g. Student for your student class) Also it is recommended to namespace your classes with your modulename (e.g. modulename.modelname)
  2. Your Many2one relation needs to point to the _name field of the other class.
  3. You are relating to a field of a different type.

You can find more information here: https://www.odoo.com/documentation/14.0/developer/howtos/backend.html

Upvotes: 3

arryph
arryph

Reputation: 2825

You don't have to set another type of field for related fields, it should be the same, in this case selection.

 gender = fields.Selection(related="student_id.gender")

Upvotes: 0

Related Questions