On 9/9/05, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> I want it so that I can do some model specific object composition after
> it loads from the db, ie if field 'type' is set to XXX then instantiate
> and attach a XXXHandler and if it's set YYY then instantiate a
> YYYHandler.

Generally, that sort of stuff can be accomplished with custom model
methods. Just do something like this:

class Foo(meta.Model):
    # ....
    def get_handler(self):
        if self.type == 'XXX': 
            return XXXHandler()
        elif self.type == 'YYY':
            return YYYHandler()

For performance, you can cache the result on a per-instance basis:

class Foo(meta.Model):
    # ....
    def get_handler(self):
        try:
            return self._handler_cache
        except AttributeError:
            if self.type == 'XXX': 
                handler = XXXHandler()
            elif self.type == 'YYY':
                handler = return YYYHandler()
            self._handler_cache = handler
            return handler

> Also, while I am at it, what is the "django-ish" way to subclass models
> and have subclasses come out on the load? This is the inheritance (as
> opposed to composition) approach which is not appropriate in this case
> but might be in another one.

I'm not sure exactly what you mean, but have you seen this?
http://www.djangoproject.com/documentation/models/subclassing/

Adrian

-- 
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Reply via email to