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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/aQQojIweoPoJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to