Retrieving dictionary values from table

Hi, I read in another forum topic that storing information in a table might be made more efficient by lumping several items together in a dictionary rather than having separate columns. I tried adding a simple object column to my table and putting ‘name’, ‘email’ and ‘address’ in a dictionary.

I can retrieve the dictionary item to populate the repeating panel but I would like it to look more like this:

Name: Someone
Email: someone@email.com
Address: 123 High Street, Town, Postcode

Is it possible to specifying this layout when loading the dictionary item into the repeating panel?

Thank you.


The good news is yes, you can do that. The bad news is that you have to do it yourself.

In the row template, self.item[‘data’] (or whatever name your simple object column has) contains the dictionary. You can drill into that to pull out whatever data you want and put it in to whatever UI elements you want. For example, if you had a label named self.name, you could use self.name.text = self.item['data']['name'].

You can drag any layout components you want into the row template columns, so you could put a column panel into the Contact Details column, and then drag in flow panels and labels as needed.

1 Like

Thank you for your previous answer. I have tried to get this to work before asking more questions here but can’t seem to get it working right. When I click on the view/edit button (picture 1), it opens up a new page/form (picture 4) and I can successfully extract one item from the dictionary and show it in the form (picture 4 Contact Details), but when I try to extract more then one dictionary item like this, it does not work: self.text_box_contact_details.text = contact_dic [‘Name:’] [‘Email:’] [‘Address:’]

Thanks

2022-02-21 (2)

If you want to pull multiple pieces of data from your dictionary into a single label, you’ll need to format that in some fashion. F strings are probably easiest, e.g.

self.text_box_contact_details.text = f"{contact_dic ['Name']} {contact_dic ['Email']}"

You can put as many fields as you want into a single label that way.

2 Likes

Perfect, thank you. That works perfectly.

1 Like

Jay already covered all, but I would like to add two little details. Pretty useless and unsolicited, but… I just had to:

  • You can do this both on code with self.name.text = self.item['data']['name'] and with data binding by putting self.item['data']['name'] on the data binding expression. Following Jay’s suggestion and doing it from code makes things more readable, but if you like using data binding, I just wanted to point out that you can do it. It’s just matter of style
  • It’s common convention to use lowercase keys for dictionaries, something like contact_dic['address'] rather than contact_dic['Address:']. You can obviously use whatever you like as a key, it’s just matter of style, and I just wanted to point it out because you are starting using dictionaries to store data

Thank you for that. All information is useful, particularly as I am quite new to Python.