3
在django中使用泛型外键的抽象基类和泛型关系有什么优缺点?在django中抽象基类与泛型关系
抽象基类隐含了一个包含子类的抽象类。这里有一个例子:
class CommonInfo(models.Model):
...
class Meta:
abstract = True
ordering = ['name']
class Student(CommonInfo):
...
class Meta(CommonInfo.Meta):
db_table = 'student_info'
通用的关系是在一个表上使用通用外键与对象ID的实体。这里是一个例子:
class TaggedItem(models.Model):
tag = models.SlugField()
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
什么是情况和/或什么时候应该使用一种解决方案或其他标准?
@Daniel Roseman你对此有何看法?一个或另一个是更好的方法? – Atma