Best way to get value one field to update another field

168 views Asked by At

I have a table A

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

My model definition looks like this:

   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",),
                    )

What I want is when user fill in the code Field, say 2001. Since 2001 is in Table A, it should give me a drop-down in priority field showing Sig1, Sig2 and Sig3 of chosing_opt, and if 2002 in code, then a drop-down in priority field showing Val1, Val2 and Val3 of chosing_opt.

Please suggest. Thanks

1

There are 1 answers

0
AudioBubble On

The structure is a bit different in your code so you have to work to convert it into something that is easy to ready code-wise (although this is not the only way - you could use for/while loops).

The idea is to convert the values like 2001 and 2002 into keys and the next tuples into their respective values.

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))

Finally, test it as follows:

T = str
get_drop_downs(chosing_opt, '2001')

Your results will look like this:

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']

Note: Not sure what T is in your code, so I pre-empted it by converting it to string (by using this as the first line T = str) for my own testing so the results show accordingly