如何在admin中将允许两种不同的内容类型作为参数

时间:2014-06-17 21:17:23

标签: django feincms

我是Django和DB noob,但我正在一个同时拥有博客和文章的网站上工作。它们在管理员中在各自的字段中实例化,基本上我有一个类" FeaturedPost"在model.py中,其中一个属性" content"我希望能够从可用的博客或文章中挑选。

我知道如果我想将内容映射到博客,我会做一个

models.ForeignKey(Blogs, related_name="w/e")

但我如何抽象这个,以便从两种内容类型中挑选? GenericForeignKey会有帮助吗?

如果在这种情况下有任何帮助,我会使用Fein-CMS。

1 个答案:

答案 0 :(得分:0)

正确,GenericForeignKey正是您所需要的。即。

from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType

class FeaturedPost(models.Model):
    ...
    content_type = models.ForeignKey(ContentType)
    content_object_id = models.PositiveIntegerField()
    content = GenericForeignKey('content_type', 'content_object_id')

要在管理员中修改这些内容,您需要使用generic inline