Thanks for the reply! I'll add the info property. Glad to see I wasn't going about this completely the wrong way
On Sun, Feb 23, 2014 at 1:16 PM, Michael Bayer <mike...@zzzcomputing.com>wrote: > > On Feb 23, 2014, at 12:59 AM, Josh Kuhn <deontologic...@gmail.com> wrote: > > > I'm writing some code to serialize some SA models to JSON, and for > columns and relationships, it's convenient to tag which fields should be > serialized with the info dictionary like so: > > > > class Thing(Base): > > id = Column(Integer, primary_key=True, info={'jsonify': False}) > > name = Column(String, info={'jsonify': True}) > > relationship(SomeModel, info={'jsonify': True} > > > > Then in the Base class, you can iterate over properties like: > > > > for attr, column in inspect(self.__class__).columns.items(): > > if column.info.get('jsonify', False): > > json_output[attr] = getattr(self, attr) > > for attr, rel in inspect(self.__class__).relationships.items(): > > # similar, with some recursion depending on rel.uselist etc... > > > > But for association_proxies, there is no info property, nor is there any > mapper.association_proxies attribute. > > > > I can obviously hack around this in some way, but I'm wondering if I'm > going about it wrong. Is the best way to get the association_proxies from > the mapper to filter through .all_orm_descriptors? > > yes, from that point of view an association proxy is in a different class > of attribute than something like a column or relationship. The hierarchy > of “attributes that are mapped” is called MapperProperty. Association > proxy isn’t in that hierarchy, but the .all_orm_descriptors collection was > added so that things like hybrid attributes and association proxies can be > found, based on their membership in the much more general “_InspectionAttr” > hierarchy. > > > > > Also, why don't association_proxies have an info property? > > no particular reason. Do you think .info would be independent of the > .info that’s present on the proxies attributes? Or it could have a copy > (or be the same collection) as that of the “left” or “right” attributes its > proxying (probably not though). Perhaps we’d want to have .info, > .local_info, .remote_info. It might be something that we just want to > stick on the base “_InsepctionAttr” so that everything gets an info, hybrid > attributes, etc. > > > > > > What would be the recommended way to do this kind of annotation on the > models for json serialization? > > Probably assume we’ll add .info to these attributes and for now just tack > an .info dictionary onto your association proxy. > > I’ve added > https://bitbucket.org/zzzeek/sqlalchemy/issue/2971/move-info-from-mapperproperty-down-tofor > that. > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.