[PonyORM-list] to_json and to_dict ssems to unable to work with partial selections

Alexander Kozlovsky alexander.kozlovsky at gmail.com
Mon Jan 5 19:28:34 UTC 2015

Hi Vadim!

Thanks for the suggestion!

Unfortunately I don't think that the `to_dict` and `to_json` methods of the
`pony.orm.serialization` module could be extended to allow specifying
tuples and other structures besides full ORM objects. The format of the
result of these methods just doesn't have place to store such a data. I'd
say that these module with its functions was just an initial experiment of
bringing Pony ORM data to the frontend side.

But now we have something better! We're almost finished development of new
serialization module which allows using not just JSON data, but full-blown
ORM objects on the frontend side!

With this new library you can do the following:
1) Construct arbitrary JSON-like data on the backend side, including not
just primitive JSON data types, but also full-blown ORM objects with fields
and relations.
2) Serialize this data to normal JSON format and pass it to the frontend
3) Get the same structures and ORM objects on the JavaScript side (with
related ORM objects if necessary)
4) Perform two-way binding of ORM objects with the HTML UI interface, so
you can change object attribute and have corresponding HTML element changed
5) Modify any object attributes or create new ORM objects on the JavaScript
side locally. The only restriction is that you cannot make direct queries
to the database (which is obvious, because this is frontend, not backend).
6) Load additional objects if necessary by performing additional AJAX calls
to the backend.
7) Automatically construct a JSON data packet with all modifications and
send it back to the server.
8) Apply changes on the server side and complete transaction.

I think that this new library is much more usable then the previous
pony.orm.serialization module. This library will be part of the next Pony
ORM release. Currently we can show you a preview with some demo example
which demonstrated all the steps I just described above.

I think that these library can be a very useful in your case. Currently it
doesn't allow sending of partial objects from the server, but I think that
we can add this feature relatively easily. Please see the demo which we a
going to send in the following message.

I need to warn that this is an early preview and not all may works as
expected. The main functionality that is omitted is access right control,
so this is not safe to use this library at the public frontend site. But it
is already very useful for the internnal admin interfaces. We plan to add
access right control in the nearest time so this library can be used at the
public frontend as well.

I'm very interested to see your comments.

Alexander Kozlovsky

On Mon, Jan 5, 2015 at 6:23 PM, Вадим Бажов <vadim at ideco.ru> wrote:

> 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 ==
>> rcodeid))
>> 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.
> _______________________________________________
> ponyorm-list mailing list
> ponyorm-list at ponyorm.com
> /ponyorm-list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </ponyorm-list/attachments/20150105/3d5c9ec9/attachment.html>

More information about the ponyorm-list mailing list