Dear all,

I am playing around with model inheritance introduced in django 1.0.
In particular, I am trying to achieve something that might be called
dynamic-casts (with reference to C++). Consider the following example
type hierarchy:

    MediaObject
    |
    +-- ImageObject
    |   |
    |   +-- PngObject
    |   +-- GifObject
    |
    +-- AudioObject
        |
        +-- Mp3Object
        +-- WavObject


I want to retreive a set of MediaObjetcs from the database and treat
them with respect to their actual type. E.g. I want to display them
using specialized templates, like

    {% for object in objects %}
        {% include object.template %}
    {% endfor %}

(or using some more sophisticated template dispatcher).

The problem I am running into is the following: when retrieving
MediaObjects from the database, e.g. via

    obj = get_objet_or_404(MediaObject,id=1)

they become upcasted to actual MediaObjects no matter what their
type has been on object creation, and I cannot pass the correct type
-- get_object_or_404(Mp3Object,id=1) -- nor access the child object as
the attribute obj.mp3object -- simply because the actual type is
unknown
on retrieval.

I tried to use the contenttype application and hold a reference to the
original objects type in MediaObject:

    class MediaObject(models.Model) :
        final_type = models.ForeignKey(ContentType)

        def __init__(self,*args) :
            super(MediaObject,self).__init__(*args)
            self.final_type =
ContentType.objects.get_for_model(type(self))

    class AudioObject(MediaObject) :
        pass

so that I could dynamically upcast objects to their original type with

    obj =
get_object_or_404(MediaObject,id=1).final_type.get_object_for_this_type(id=1)

It does not work at all. For some reason, final_type also gets
upcasted
to MediaObject?!

Is there any (possibly elegant) way to achieve this functionality?

Thanks!

- harold -

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" 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/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to