Why not define a metaclass that does the mixins for you?
class UseMixins(db.Model):
__metaclass__ = AddMixin
class Post(UseMixins):
Mixins = Comment,
Some code that might be useful in helping you write AddMixin can be
found in:
http://groups.google.com/group/google-appengine/browse_thread/thread/cf9af7c9a5976d27/0e50aad5476a6397?lnk=gst&q=addkeyreference#0e50aad5476a6397
If you figure out how to eliminate the explicit reference to db.Model,
please let me know.
On Nov 17, 8:38 am, Adam <[EMAIL PROTECTED]> wrote:
> I'm wondering if it is possible to add a field to a Model class with a
> mixin.
>
> Here's my hypothetical problem: let's say that I am designing Blog
> software -- I know, I know, what an innovation! -- that features Posts
> and Comments.
>
> class Post(db.Model):
> title = db.StringProperty(required=True)
> body = db.TextProperty(required=True)
> added = db.DateProperty(auto_add_now=True)
>
> class Comment(db.Model):
> author = db.UserProperty()
> body = db.TextProperty()
> added = db.DateProperty()
> edited = db.DateProperty()
>
> I would like Post to have a counter of the number of comments that it
> has, so I could change my definition of Post to this:
> class Post(db.Model):
> title = db.StringProperty(required=True)
> body = db.TextProperty(required=True)
> added = db.DateProperty(auto_add_now=True)
> comments_counter = db.IntegerProperty(required=True, defualt=0)
>
> However, what I would really to do is have a mixin class called
> Commentable that will add a variety of useful methods to the class
> that mixes it in. It'd be super cool to be able to have
> comments_counter added in by it:
>
> class Commentable:
> comments_counter = db.IntegerProperty(required=True, default=0)
>
> # Various method definitions excluded for brevity...
>
> class Post(db.Model, Commentable):
> title = db.StringProperty(required=True)
> body = db.TextProperty(required=True)
> added = db.DateProperty(auto_add_now=True)
> # comments_counter = db.IntegerProperty(required=True, defualt=0)
> # Now, comments_counter is included by Commentable mixin
>
> I gave something like this a try last night, and it didn't seem to
> work, so I figured that I'd ask before going too-deeply down a rabbit
> hole.
>
> If this won't work, I think that it'd be super cool. It would enable
> a wide range of work-and-time saving plugins, such as in the Ruby on
> Rails ecosystem.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google App Engine" 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/google-appengine?hl=en
-~----------~----~----~----~------~----~------~--~---