views:

23

answers:

1

Hello!

I have a models in different files (blog/models.py, forum/models.py, article/models.py). In each of this files I have defined model classes with application prefix (BlobPost, BlogTag, ForumPost, ForumThread, Article, ArticleCategory).

Also I have appliation - comment, for adding comment attached to any model object. For example, I want to comment BlogPost, or add comment referenced to ForumPost. For this I use property with type ReferenceProperty() - without specify type of references. Any model can attached to comment.

What a problem? If I have show all comments in administration section, I see a problem with autoloading models for ReferenceProperty. I don't know, what type of model used for current comment. I need to autoload package with model, if this need.

Yes, exists simple solution - include all models from all applications. But, this is not good solution. I need load only need models. How to do this autoloading?

My idea is based on detect kind of property, and by first part of this name detect application name for load all models in this application. For example, I have comment with Reference to BlogPost model. I get name of application - Blog and load all models from blog.models import *

For implement my idea I need to understand - how to intercept creating property instances. In my case, if I loop over comments, I see that App Engine automatically (thanks, but not in my case) create instances for properties.

How to inject my logic for loading my models before creating property instance?

Thank you!

+1  A: 

This isn't possible in the standard db framework, as there's not enough information present to find your models. The only information the framework has to work with is the kind name, which doesn't include the fully qualified package - so it has no way to figure out what package your model definition might be in.

If you're writing an admin interface, though, you probably want to use the low-level google.appengine.api.datastore interface, instead, which operates on dicts instead of model classes, and doesn't require a model definition.

Nick Johnson
Thank you Nick!I have found solution. I have load all models in first request, and cache this value for all next requests. This is help me save time for load all models, and also I have loaded all models in memory.Such as my models have a unique prefix with model name (BlogPost, ForumComment, etc) - we have not problem with equal model names.Problem was solved by me in AppEngine framework.Thank you for help!
Anton Danilchenko