在关系字段odoo 10的计算中检索字段值

时间:2018-08-24 13:40:59

标签: python odoo

我想在Many2one字段中检索模型记录的所有值。我不知道是否可以使用计算机。

我的班级要恢复该值:

class ResPartner_school(models.Model):

    _name = 'ecole.partner.school'
    _order = 'id desc'

    school_name = fields.Many2one(comodel_name="ecole.establishment.webservice",
                              string="Etablissement Scolaire",
                               default=1)
    school_level = fields.Many2one(comodel_name="ecole.establishment.webservice",
                               string="Niveau Scolaire",
                               compute="_get_level")

我的另一堂课:

class SchoolEstablishmentWebServices(models.Model):

    _name = 'ecole.establishment.webservice'
    _rec_name = "establishment_name"

    establishment_name = fields.Many2one(comodel_name="horanet.school.establishment", string="Etablissement Scolaire")
    id_establishment = fields.Char(string='idEtablissement')
    grade_name = fields.Many2one(comodel_name="horanet.school.grade", string="Niveau Scolaire")
    id_class = fields.Char(string='idClasse')

我在班级ResPartner_school中的功能:

    @api.multi
    def _get_level(self):
        school_level = self.school_name.grade_name

        return school_level

如何从类grade_name的{​​{1}}字段中检索所有值?

1 个答案:

答案 0 :(得分:0)

一种显示“所有”数据(取决于数据量)的解决方案是使用两个变体覆盖模型的name_get()

  1. 基于变体上下文的name_get()

首先覆盖模型name_get()的{​​{1}}

ecole.establishment.webservice

然后,您需要将值class SchoolEstablishmentWebServices(models.Model): _name = 'ecole.establishment.webservice' @api.multi def name_get(self): res = [] for webservice in self: if 'full_name' in self.env.context: res.append((webservice.id, webservice.get_full_name())) else: res.append((webservice.id, webservice.establishment_name)) return res def get_full_name(self): # example logic self.ensure_one() full_format = "{establishment}, {grade}" return full_format.format( establishment=self.establishment_name, grade=self.grade_name) 放入上下文中。您可以将其添加到字段本身,但效果不佳。最好将值放在菜单操作的上下文中,该菜单用于显示full_name条目。

'ecole.partner.school'
  1. 不基于上下文的变体->与变体1相同,完全不使用上下文。它不仅会在您希望的位置出现在系统范围内。这是更容易的变体。