django model meta类定义abstract = True

class TimeStampedModel(models.Model):
"""
An abstract base class model that provides selfupdating
``created`` and ``modified`` fields.
"""
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
class Meta:

abstract = True

子类继承 TimeStampedModel

from core.models import TimeStampedModel
class Flavor(TimeStampedModel):
title = models.CharField(max_length=200)

运行migrate时,只会创建一个table Flavor;如果TimeStampedModel不是table ,那么运行migrate时,将会创建2个table;而且Flavor不会有父类TimeStampedModel的created 和modified 字段,需要通过外键关联;而且操作Flavor实例的时候,也会影响TimeStampedModel表


但是注意一点:

concrete inheritance has the potential to become a nasty performance bottleneck. This is even more true when you subclass a concrete model class multiple times.
Further reading:
http://2scoops.co/1.8-model-inheritance

你可能感兴趣的:(django)