在 Django 管理中,我可以使用 GenericTabularInline 内联显示通用关系的反转.这行得通....
class Art(TimeDateStampedModel):
collections = GenericRelation('apps.Collection')
admin.py
class CollectionsInline(GenericTabularInline):
model = Collection
class ArtAdmin(admin.ModelAdmin):
inlines = [
CollectionsInline,
MediaActivityInline,
]
但它只允许我添加新的集合或更新已经相关的集合。 如何从 Art 中搜索/选择或挑选现有的Collection。
我已经尝试在 Art this 上使用 GenericRelation 但它不起作用:
class ArtAdmin(admin.ModelAdmin):
fieldsets = (
('', {
'fields': ('collections'),
}),
)
请您参考如下方法:
您当前在Collection 模型中有一个通用外键。听起来这不是您想要的,因为您希望能够将每个 collection 分配给多个外部对象。
您可以尝试创建一个模型,例如CollectionMember(不是一个好名字),带有一个集合外键和一个通用外键。
class CollectionMember(models.Model):
"""
Allows collections to be assigned to multiple external
objects using a generic foreign key
"""
collection = models.ForeignKey(Collection)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
在您的 ArtAdmin 中将 CollectionMember 作为通用内联添加,然后您将能够为每个内联 collection_member 选择集合。
