在django中使用通用外键的抽象基类和泛型关系的优缺点是什么?
抽象基类意味着具有子类的单个抽象类 . 这是一个例子:
class CommonInfo(models.Model):
...
class Meta:
abstract = True
ordering = ['name']
class Student(CommonInfo):
...
class Meta(CommonInfo.Meta):
db_table = 'student_info'
通用关系是在单个表上使用具有对象标识的通用外键的实体 . 这是一个例子:
class TaggedItem(models.Model):
tag = models.SlugField()
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
何时使用一种解决方案或另一种解决方案的情况和/或标准是什么?
1 回答
经过一番研究后,我认为抽象基类范式与自然关系数据库架构更加一致 . 通用关系更多是黑客攻击并绕过参照完整性 .
抽象基类是要走的路 .