使用contenttypes在Django中创建FeaturedContent功能

2024-10-03 00:28:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用contenttypes框架在我的网站上创建一个“特色内容”功能。我基本上是这样定义一个模型的:

class FeaturedContent(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

我现在想做的是在我的管理区域内的每个模型编辑/创建页面上都有一个勾选框,当勾选并提交时,它会将内容引用添加到FeaturedContent。取消勾选时,引用也会被删除。你知道吗

如果有更好的方法,请让我知道。在我看来,使用contenttypes是一个不错的选择。你知道吗

非常感谢!你知道吗


Tags: 模型功能框架id内容定义object网站
1条回答
网友
1楼 · 发布于 2024-10-03 00:28:18

您将需要创建一个StackedLine管理,为您的每个模型,需要在管理中的这个选项。你知道吗

如下所示:

class ObjectInline(admin.StackedInline):
    model = YourFancyModelthatisFeatured
    extra = 0

class FancyModelAdmin(admin.ModelAdmin):
    inlines = [ObjectInline]

但是这将在默认小部件中提供内联线,因此您还需要定义一个表单来定制checkbox小部件。你知道吗

相关问题 更多 >