【问题标题】:in odoo can i relate Many2one relation to a specific field apart from _rec_name在odoo中,我可以将Many2one关系与_rec_name之外的特定字段联系起来吗
【发布时间】:2020-03-31 07:08:56
【问题描述】:
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)

这里不是 course_id ,我需要大学模型中的 course_name 。我试过 'fields.Many2one('module2_course.course_name',"String")' 但它显示没有找到名称的表。

【问题讨论】:

    标签: python-3.x odoo erp odoo-12


    【解决方案1】:

    试试这个方法:

    course_name = fields.Selection(related=stream.course_name)
    

    【讨论】:

      【解决方案2】:

      相关字段使用字段引用本身工作。因为您的代码引用 objectname.field_name 这将不起作用。所以应该是 field_name.field_name

      【讨论】:

        【解决方案3】:

        相关字段与其他字段一样工作,但在后台是通用计算字段。只需定义与相关字段具有相同字段类型的字段即可。

        在你的情况下是:

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

        你应该尽量坚持Odoo programming and naming guidelines。例如course_id 坚持下去,但stream 没有。它应该命名为stream_id 或更好的名称(但可能与上下文无关)course_id

        【讨论】:

        • 试过但得到 AssertionError:Field without Selection
        • 我心里有这个想法,但好久没做过这样相关的领域了。您已将选择再次设置为相关字段中的相同选择。我稍后会编辑我的答案。
        • 如果我想要相同的选择字段,如何将其关联到不同的选择字段
        • 我现在已经编辑了我的答案,你只需要复制选择。您还可以使用返回选择的方法,这样更易​​于维护。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-09
        • 2015-10-24
        相关资源
        最近更新 更多