开发者

Figure out child type with Django MTI or specify type as field?

I'm setting up a data model in django using multiple-table inheritance (MTI) like this:

class Metric(models.Model):
    account = models.ForeignKey(Account)
    date = models.DateField()
    value = models.FloatField()
    calculation_in_progress = models.BooleanField()
    type = models.CharField( max_length=20, choices= METRIC_TYPES ) # Appropriate?

    def calculate(self):
        # default calculation...

class WebMetric(Metric):
    url = models.URLField()

    def calculate(self):
        # web-specific calculation...

class TextMetric(Metric):
    text = models.TextField()

    def calculate(self):
        # text-specific calculation...

My instinct is to put a 'type' field in the base class as shown here, so I can tell which sub-class any Metric object belongs to. It would be a bit of a hassle to keep this up to date all the time, but possible. But do I need to do this? Is there some way that django handles this automatically?

When I call Metric.objects.all() every objects returned is an instance of Metric never the subclasses. So if I call .calculate() I never get the sub-class's behavior.

I could write a function on the base class that tests to see if I can cast it to any of the sub-types like:

def determine_subtype(self):
    try:
        self.webmetric
        return WebMetric
    except WebMetric.DoesNotExist:
        pass
    # Repeat开发者_Python百科 for every sub-class

but this seems like a bunch of repetitious code. And it's also not something that can be included in a SELECT filter -- only works in python-space.

What's the best way to handle this?


While it might offend some people's sensibilities, the only practical way to solve this problem is to put either a field or a method in the base class which says what kind of object each record really is. The problem with the method you describe is that it requires a separate database query for every type of subclass, for each object you're dealing with. This could get extremely slow when working with large querysets. A better way is to use a ForeignKey to the django Content Type class.

@Carl Meyer wrote a good solution here: How do I access the child classes of an object in django without knowing the name of the child class?

Single Table Inheritance could help alleviate this issue, depending on how it gets implemented. But for now Django does not support it: Single Table Inheritance in Django so it's not a helpful suggestion.


But do I need to do this?

Never. Never. Never.

Is there some way that django handles this automatically?

Yes. It's called "polymorphism".

You never need to know the subclass. Never.

"What about my WebMetric.url and my TextMetric.text attributes?"

What will you do with these attributes? Define a method function that does something. Implement different versions in WebMetric (that uses url) and TextMetric (that uses text). That's proper polymorphism.


Please read this: http://docs.djangoproject.com/en/1.2/topics/db/models/#abstract-base-classes

Please make your superclass abstract.

Do NOT do this: http://docs.djangoproject.com/en/1.2/topics/db/models/#multi-table-inheritance

You want "single-table inheritance".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜