-1
class College(models.Model):
_name = 'module2_college'
_description = 'College Info'
_rec_name = 'clg_name'
clg_name = fields.Char("College")
stream = fields.Many2one('module2_course',"Course")

class Course(models.Model):
_name = 'module2_course'
_description = 'Course Information'
_rec_name = 'course_id'

course_name = fields.Selection([
    ('1', 'BTECH'),
    ('2', 'MTECH'),
    ('3', 'MCA')
],"Stream")
course_id = fields.Char("Course ID")

semester = fields.One2many('module2_semester','cou_id',"Semesters",required=True)

Here instead of course_id ,i need course_name in college model. I tried 'fields.Many2one('module2_course.course_name',"String")' but it shows no table found of name.

Abhishek S
  • 27
  • 6

3 Answers3

0

try this way:

course_name = fields.Selection(related=stream.course_name)
Pruthvi Barot
  • 1,948
  • 3
  • 14
0

Related fields just work like other fields, but are generic computed fields in the background. Just define those fields with the same field type like the related one.

In your case it would be:

course_name = fields.Selection(selection=[
    ('1', 'BTECH'),
    ('2', 'MTECH'),
    ('3', 'MCA')
], related="stream.course_name")

You should try to stick to the Odoo programming and naming guidelines. As example course_id is sticking to it, but stream doesn't. It should be named stream_id or better (but maybe out of context) course_id.

CZoellner
  • 13,553
  • 3
  • 25
  • 38
  • tried but got AssertionError:Field without Selection – Abhishek S Apr 01 '20 at 15:52
  • Had that in my mind, but hadn't done such a related field in ages. You have set the selection again as the same selection in the related field. I will edit my answer later. – CZoellner Apr 01 '20 at 16:06
  • How do I'll relate it to different selection field if I want the same one – Abhishek S Apr 01 '20 at 17:16
  • I've edited my answer now, you just need to copy the selection. You could also work with methods returning the selection, that would be a bit more maintainable. – CZoellner Apr 02 '20 at 07:09
0

Related fields work using field reference itself.. As your code refer to objectname.field_name which will not work. So it should be field_name.field_name

Dharman
  • 30,962
  • 25
  • 85
  • 135