Model Inheritance Downcasting

64 views
Skip to first unread message

Bastian Kuberek

unread,
Nov 16, 2011, 11:43:30 AM11/16/11
to django...@googlegroups.com
Hi,

I have looked a lot and have not yet figured out how to accomplish this.

Let me show with an example what I am trying to accomplish.


class WidgetType(models.Model):
    """WidgetType categorizes the type of Widget"""

    name = models.CharField(max_length=255)
    slug = models.SlugField(max_length=255, editable=False)

    class Meta:
        db_table = u'widget_type'

class Widget(models.Model):
    """Widget is a component of WidgetType type that renders a DataStream on a defined Dashboard"""

    widget_type = models.ForeignKey(WidgetType)

    name = models.CharField(max_length=255, null=True, blank=True)

    class Meta:
        db_table = u'widget'

class LineChart(Widget):
    """Line Chart Widget Proxy"""

    class Meta:
        proxy = True

    def save(self, *args, **kwargs):
        self.widget_type = WidgetType.objects.get(slug='line-chart')
        super(LineChart, self).save(*args, **kwargs)

As you can see above, there are 3 classes (simplified). I have 2 tables: "widget_type" and "widget". When creating an instance of Widget, one must select the type of widget. So one could create instances of LineChart, BarChart, ScatterChart, etc and all of this models would persist to the same table, "widget".

The issues I have with this design is that one doesn't always know what type of widget it is so the following is not possible:

widgets = LineChart.objects.all() 

What I need is to be able to query the Widget class and get back subclasses based on their type:

>>> widgets = Widget.objects.all()
>>> print widgets
[<LineChart: 1>,<LineChart: 2>,<LineChart: 3>,<BarChart: 4>,<lineChart: 5>,<ScatterChart: 6>]


Any help appreciated.

Tom Evans

unread,
Nov 16, 2011, 11:59:51 AM11/16/11
to django...@googlegroups.com
On Wed, Nov 16, 2011 at 4:43 PM, Bastian Kuberek <bkub...@gmail.com> wrote:
> Hi,
> I have looked a lot and have not yet figured out how to accomplish this.
> Let me show with an example what I am trying to accomplish.
>
> <snip description of polymorphism>

http://code.google.com/p/django-polymorphic-models/

Cheers

Tom

Torsten Bronger

unread,
Nov 16, 2011, 12:03:11 PM11/16/11
to django...@googlegroups.com
Hall�chen!

Bastian Kuberek writes:

> [...]


>
> What I need is to be able to query the Widget class and get back
> subclasses based on their type:
>
>>>> widgets = Widget.objects.all()
>>>> print widgets
> [<LineChart: 1>,<LineChart: 2>,<LineChart: 3>,<BarChart: 4>,<lineChart:
> 5>,<ScatterChart: 6>]

We do this very reliably with
http://djangosnippets.org/snippets/2091/ but I find
https://github.com/carljm/django-model-utils/blob/master/README.rst
very interesting, too.

This is a frequently-requested feature with many implementations,
yet there is no canonical solution in Django, unfortunately.

Tsch�,
Torsten.

--
Torsten Bronger Jabber ID: torsten...@jabber.rwth-aachen.de
or http://bronger-jmp.appspot.com

Reply all
Reply to author
Forward
0 new messages