Creating a dict from a table record

Is there a convenient way to convert a record from a data table into a python dict?

I have a form which takes various user input fields, binds those to self.item and passes the result to a server module to update the data table with some validation on the way. So far, so good.

However, I’d like to initialise the form with the current record. I can extract the relevant record and hand crank it into the self.item dict by listing the fields, but it would be far nicer to do so directly.

Hi Owen,

Ah, yes. This is a slightly tricky corner. The short version is that you can call dict(row) on a data table row, and it will work…on the server. On the client, due to a corner case in the implementation of the Python-to-Javascript library we use, you have to use dict(list(row)) (else you’ll get a “cannot block or suspend here” error). This is…not ideal, and we do plan to fix this.

tl;dr dict(list(row)) (or just dict(row) on the server)

4 Likes

Aha! thank you!

I hit the ‘cannot block…’ error and gave up. I shall add the little necessary extra

So… when attempting to debug with

print repr(row)

we should use

print(dict(list(row)))

or

print(repr(dict(list(row))))

instead?