获取一个字段的值以更新另一个字段的最佳方法

2024-04-24 20:01:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一张桌子

Table A:
---------------------------------------
id |    valueName    |    value
---------------------------------------
1  |    2001         |    Nepal
---------------------------------------
2  |    2002         |    Thailand
---------------------------------------

我的模型定义如下:

   chosing_opt = ("2001", [
                ("Sig1", T("Sig1"), "I", "E", "O"),
                ("Sig2", T("Sig2"), "E", "S", "O"),
                ("Sig3", T("Sig3"), "E", "M", "O")
                ],
        "2002", [
                ("Val1", T("Val1"), "I", "E", "O"),
                ("Val2", T("Val2"), "E", "S", "O"),
                ("Val3", T("Val3"), "E", "M", "O")
                ],
        )

        define_table(tablename, 
                     Field("priority",),
                     Field("code", "list:string",),
                    )

我想要的是当用户填写code字段时,比如说2001。由于2001在表A中,它应该在priority字段中提供一个下拉列表,显示chosing_opt的Sig1、Sig2和Sig3,如果在code中提供2002,那么在priority字段中提供一个下拉列表,显示chosing_opt的Val1、Val2和Val3。你知道吗

请建议。谢谢


Tags: idfield列表tablecodeoptpriority桌子
1条回答
网友
1楼 · 发布于 2024-04-24 20:01:03

代码中的结构有点不同,因此必须将其转换为易于编写的代码(尽管这不是唯一的方法-可以使用for/while循环)。你知道吗

其思想是将20012002等值转换为键,并将下一个元组转换为它们各自的值。你知道吗

def get_drop_downs(opts, val):
    # zip together (keys, values)... Keys = Starting from index 0, skip 1 tuple. 
    # Same for values, starting from index 1
    d_opts = dict(zip(opts[0::2], opts[1::2]))
    print('Just Key Names = {}'.format(opts[0::2]))  # Double check the keys
    print('The related tuples, in same order={}'.format(opts[1::2]))  # Double check the values
    results = [tpls[0] for tpls in d_opts[val]]  # every value is a tuple, so pick up the first value only
    print('Results = {}'.format(results))

最后,测试如下:

T = str
get_drop_downs(chosing_opt, '2001')

结果如下所示:

Just Key Names = ('2001', '2002')
The related tuples, in same order=([('Sig1', 'Sig1', 'I', 'E', 'O'), ('Sig2', 'Sig2', 'E', 'S', 'O'), ('Sig3', 'Sig3', 'E', 'M', 'O')], [('Val1', 'Val1', 'I', 'E', 'O'), ('Val2', 'Val2', 'E', 'S', 'O'), ('Val3', 'Val3', 'E', 'M', 'O')])
Results = ['Sig1', 'Sig2', 'Sig3']

注意:不确定代码中的T是什么,因此我通过将其转换为字符串(通过将其用作第一行T = str)对其进行了抢占,以便相应地显示结果

相关问题 更多 >