A have a web application with a lot of model classes. Most models have
some field to indicated whether object should be public (displayed to
general used). When a retrieve them I have to accurately filter each
query object with proper condition and take some action (like
filtering in code) for relations. This is error prone. That's why many
web frameworks with database access have some way to filter queries
automatically: custom model manager in django, tagged stream
conditions in QPS etc. I believe this is very important feature, since
it allows to avoid one of the most frequent error, so I'm looking for
such ability in SQLAlchemy. There is a suggestion (
http://groups.google.com/group/sqlalchemy/browse_thread/thread/bcd10e4a2f5c603d/7dc1515973e5c7ba
) to provide custom query_cls. This probably worked a year ago, but
doesn't work now.
Here is query class I pass as query_cls argument to sessionmaker:
def QueryPublic(entities, session=None):
query = Query(entities, session)
# XXX I haven't ever seen examples with several entities, so I can
test
# this case.
assert len(entities)==1, entities
# XXX Sometimes it's not a model class or mapper, so the following
fails.
cls = _class_to_mapper(entities[0]).class_
public_condition = getattr(cls, 'public_condition', None)
if public_condition is not None:
query = query.filter(public_condition)
return query
This works for simple queries, but any access to relation gives an
about Query.get() used for query with condition (this wasn't a case
for 0.4.x). OK, i've redefined get() method to be less restrictive in
cost of performance:
class HackedQuery(Query):
def get(self, ident):
# XXX This works for case when primary key is constructed of
id field
# only.
if isinstance(ident, (tuple, list)):
assert len(ident)==1
ident = ident[0]
return self.filter_by(id=ident).first()
This helped for some properties access, but not all. Right now I have
a similar error for statement_from() method. Probably I can fix this
too, but how many methods I have to overwrite to make it working
ever?
It looks like I'm going wrong way. Any suggestion?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---