[PonyORM-list] to_json and to_dict ssems to unable to work with partial selections
vadim at ideco.ru
Mon Jan 5 15:23:02 UTC 2015
I've got a clue, I suppose.
When we select or get a whole entity(ies), pony outputs a collection or
structure, or something similar (arrays ?) representing the entity(ies)
in DB. It holds the structure of entities and is easily convertable to
json. So to_json converts this to json string.
When we make a partial selection of fields - tuple is being returned,
but to_json is waiting for a db model representation. It's easy to
convert to json too, but that's another story )).
So, this thread is kinda a feature request: please make to_json and
to_dict are able to work with tuples for partial selections too.
Built-in json answers from DB backend is very useful feature to let Pony
talk directly in RESTfull API with web-frontend.
On 02.01.2015 14:51, Вадим Бажов wrote:
> Suppose we have a table of Codes and Customers. Each code can belong
> to exactly one customer.
> For example, this works fine, giving me a json representation of a
> whole object with all relation onjects if any:
> rcodeid = '3'
> rcode = to_json(select(r for r in Rcodes if r.id == rcodeid))
> But if i try to select particular fields from table, like this:
> rcode = to_json(select((r.id,r.code) for r in Rcodes if r.id ==
> i get an exception:
> (<type 'exceptions.TypeError'>, TypeError('not all arguments converted
> during string formatting',), <traceback object at 0x8ff648c>)
> The situation with to_dict is exactly the same. Works fine with full
> entities, throws an exception with partial selections.
> Didn't find any notice in docs and blog that to_dict and to_json works
> only with complete entities. So enlighten me please if it's me doing
> something wrong or Pony ))
> Thank you for the to_json method - it's very usefull.
More information about the ponyorm-list