[sqlalchemy] association_proxy as property?

2010-11-16 Thread A . M .
Hello,

To generate json from our SQLAlchemy model objects, we are using 
iterate_properties to determine how to dictify the object. One of our objects 
uses association_proxy which we would like to represent in the JSON. 
Unfortunately, because it is not a property, the dictification misses this 
property. I feel like I am missing something simple here.

How can I make an association_proxy appear to be a property which appears in 
iterate_properties of the mapper?

Cheers,
M


-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] association_proxy as property?

2010-11-16 Thread Michael Bayer

On Nov 16, 2010, at 11:14 AM, A.M. wrote:

 Hello,
 
 To generate json from our SQLAlchemy model objects, we are using 
 iterate_properties to determine how to dictify the object. One of our 
 objects uses association_proxy which we would like to represent in the JSON. 
 Unfortunately, because it is not a property, the dictification misses this 
 property. I feel like I am missing something simple here.
 
 How can I make an association_proxy appear to be a property which appears in 
 iterate_properties of the mapper?

1. why not use dir(myobject) to get a view of your object as a whole ?
2. implicit conversion to JSON and such is a little sloppy.   You'd be better 
off using a structured approach like Colander: http://docs.repoze.org/colander/


-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] association_proxy as property?

2010-11-16 Thread A.M.

On Nov 16, 2010, at 11:43 AM, Michael Bayer wrote:

 
 On Nov 16, 2010, at 11:14 AM, A.M. wrote:
 
 Hello,
 
 To generate json from our SQLAlchemy model objects, we are using 
 iterate_properties to determine how to dictify the object. One of our 
 objects uses association_proxy which we would like to represent in the JSON. 
 Unfortunately, because it is not a property, the dictification misses this 
 property. I feel like I am missing something simple here.
 
 How can I make an association_proxy appear to be a property which appears in 
 iterate_properties of the mapper?
 
 1. why not use dir(myobject) to get a view of your object as a whole ?

We swiped dictify from sprox.saormprovider:
http://bitbucket.org/percious/sprox/src/tip/sprox/saormprovider.py#cl-321

We like the behavior that a REST call to retrieve an SQLAlchemy object returns 
pks for relationships (instead of loading the entire result hierarchy), so 
subsequent REST calls are required to dig deeper into the tree of results.

Using dir() is definitely a good suggestion which I will try. You are probably 
right that we are using iterate_properties improperly.

 2. implicit conversion to JSON and such is a little sloppy.   You'd be better 
 off using a structured approach like Colander: 
 http://docs.repoze.org/colander/

It looks like I would have to either re-define all objects using the Colander 
syntax or implement a method which converts existing SQLAlchemy models to 
Colander schema objects. Even if the latter function already exists, I still 
have the problem of determining automatically which properties to encode, no?

Cheers,
M

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] association_proxy as property?

2010-11-16 Thread jason kirtland
On Tue, Nov 16, 2010 at 9:05 AM, A.M. age...@themactionfaction.com wrote:

 On Nov 16, 2010, at 11:43 AM, Michael Bayer wrote:


 On Nov 16, 2010, at 11:14 AM, A.M. wrote:
 To generate json from our SQLAlchemy model objects, we are using 
 iterate_properties to determine how to dictify the object. One of our 
 objects uses association_proxy which we would like to represent in the 
 JSON. Unfortunately, because it is not a property, the dictification misses 
 this property. I feel like I am missing something simple here.

 How can I make an association_proxy appear to be a property which appears 
 in iterate_properties of the mapper?

 2. implicit conversion to JSON and such is a little sloppy.   You'd be 
 better off using a structured approach like Colander: 
 http://docs.repoze.org/colander/

 It looks like I would have to either re-define all objects using the Colander 
 syntax or implement a method which converts existing SQLAlchemy models to 
 Colander schema objects. Even if the latter function already exists, I still 
 have the problem of determining automatically which properties to encode, no?

You may find you'll need to do even further work to determine which
properties to encode.  I do the same (using Flatland for
serialization), and part of that challenge was determining where the
edges of the business objects were.  (If you have relations, maybe
some of them are part of the object (as user's email addresses) and
some of them aren't (a User-Users list of the user's friends). In the
end I went with a combination of class annotation and heuristics based
on iterating mapper properties.  This allowed me to traverse the
mappings to reliably find the edges and also include the occasional
transient attribute or other oddball that needed to be in the
serialized form.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] association_proxy as property?

2010-11-16 Thread Michael Bayer

On Nov 16, 2010, at 12:05 PM, A.M. wrote:

 
 2. implicit conversion to JSON and such is a little sloppy.   You'd be 
 better off using a structured approach like Colander: 
 http://docs.repoze.org/colander/
 
 It looks like I would have to either re-define all objects using the Colander 
 syntax or implement a method which converts existing SQLAlchemy models to 
 Colander schema objects. Even if the latter function already exists, I still 
 have the problem of determining automatically which properties to encode, no?

Jek's suggestion is good here, also in my experience when I need to use JSON 
for something, it implies I'm communicating with some other system that doesn't 
know about my mapped objects - meaning, even if the JSON structure is initially 
driven by the ORM completely, I need the JSON structure to remain independent 
of changes in the ORM model in any case.   But usually I'm dealing with a 
service that looks dramatically different from my ORM models anyway.   Its 
again the same dichotomy in SQLAlchemy itself, your json messages are not your 
domain objects ;)

I haven't used Colander and instead have something homegrown - a core feature 
is that it maintains a mapping of object attributes, in some cases 
dot-separated paths, to field names that would be generated in JSON or XML.   I 
also use it to translate against .csv and .xls formats.   Most apps I work on 
end up that way, so I go straight to explicit mappings up front since I know 
the variability is going to be great.


-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.