我有一个 Django 模型,它可以有许多通用关系。这是通过手动创建连接模型来实现的:
class ContentProjectContentItem(models.Model):
content_project = models.ForeignKey('ContentProject')
# Generic relations via Content Type Framework
object_id = models.PositiveIntegerField()
content_type = models.ForeignKey(ContentType)
item = generic.GenericForeignKey('content_type', 'object_id')
class Meta:
unique_together = ('content_project', 'object_id', 'content_type')
app_label = "cms"
这通过 ManyToManyField 与父模型相关:
class ContentProject(models.Model):
title = models.CharField(max_length=100)
date_created = models.DateTimeField(auto_now_add=True)
# Related content (Assets, Resources, Media, etc)
content_items = models.ManyToManyField(ContentProjectContentItem)
我仍在使用 Django 管理员开发我的技能,所以我不确定如何创建允许我将多个 ContentProjectContentItems 添加到 ContentProject 的内联。我尝试过使用generic.GenericStackedInline
,但无济于事:
class ContentItemInline(generic.GenericStackedInline):
name = "content_project"
model = ContentProjectContentItem
class ContentProjectAdmin(MPTTModelAdmin):
list_display = ('title' , 'date_created')
search_fields = ('title' , 'date_created')
inlines = [
ContentItemInline,
]
我只是看到标准的 ManyToMany 多选content_items
(这是我所期望的)。我不确定如何ContentProjectAdmin
了解联结表的通用关系。有什么想法吗?