如何在Tryton中从many2one字段获取选定值

0 投票
1 回答
701 浏览
提问于 2025-04-18 16:04

我需要创建一个叫做“类别”的多对一或选择字段,还有一个叫做“级别”的字段。一个类别可以关联多个级别。这就像选择一个国家,然后根据这个国家填写与之相关的子区域。我的代码是:


categoryy=fields.Many2One("grh.category","Category")
ech = fields.Many2One("grh.echelon",'echelon', depends=[ 'categoryy'])

@fields.depends('ech', 'categoryy')
def on_change_categoryy(self):
    if (self.ech
            and self.ech.echeloncategory != self.categoryy):
        return {'ech': None}
    return {}

from trytond.model import ModelView,ModelSQL,fields

__all__ = ['echelon']


class echelon(ModelView,ModelSQL):
    '''echelon'''
    __name__ = "grh.echelon"
    echeloncategory=fields.Many2One("grh.category","echelonofcategory")
    echelon=fields.Char("Echelon")

from trytond.model import ModelView,ModelSQL,fields
from trytond.pool import Pool

__all__ = ['category']


class category(ModelView,ModelSQL):
    '''category'''
    __name__ = "grh.category"
    category=fields.Char("category")
    echelons=fields.One2Many("grh.echelon","echeloncategory","Category echelons")

我不知道为什么“级别”字段会显示所有的级别。

请帮帮我。

1 个回答

1

你应该使用一个域条件来限制你在 ech 字段中可以选择的选项。你可以在下面的链接找到关于域的详细说明:

http://doc.tryton.org/3.2/trytond/doc/topics/domain.html?highlight=domain

另外,你还需要使用 PYSON 来获取当前类别的值,所以你最终会得到类似这样的代码:

from trytond.pyson import Eval
ech = fields.Many2One("grh.echelon",'echelon', 
    domain=[
        ('category', '=', Eval('category', -1)),
    depends=['categoryy'])

你可以在下面的链接找到关于 PYSON 的介绍:

http://doc.tryton.org/3.2/trytond/doc/topics/pyson.html

撰写回答