views:

249

answers:

1

'Ello, all. I'm trying to create a model in Django based on - but not subclassing or having a DB relation to - another model. My original model looks something like this: it stores some data with a date/time stamp.

class Entry(Model):  
    data1 = FloatField()  
    data2 = FloatField()  
    entered = DateTimeField()

I'd also like to aggregate the numeric data for each of these entries on a daily basis, using a model that is almost identical. For the DailyAvg() variant, we'll use a DateField() instead of a DateTimeField(), as there'll only be one average per day:

class EntryDailyAvg(Model):  
    data1 = FloatField()  
    data2 = FloatField()  
    entered = DateField()

Thus the problem: There's going to be a lot of these data classes that will need a corresponding daily average model stored in the DB, and the definitions are almost identical. I could just re-type a definition for an equivalent DailyAvg() class for each data class, but that seems to violate DRY, and is also a huge pain in the arse. I also can't have EntryDailyAvg subclass Entry, as Django will save a new Entry base every time I save a new EntryDailyAvg.

Is there a way to automaticaly (-magically?) generate the DailyAvg() class?

Thanks in advance!

+2  A: 

What if you create a AbstractEntry class with all the data1 stuff and then, two subclasses: Entry and EntryDailyAvg.

Check the docs for info on how to tell django that one class is abstract.

Tiago
Abstract classes are new to 1.1, aren't they? Hadn't thought of that. Thanks!