开发者

Creating a FeaturedContent feature in Django using contenttypes

开发者 https://www.devze.com 2022-12-17 10:58 出处:网络
I\'m using the contenttypes framework to create a \"featured content\" feature on my site. I\'ve basically done this by defining a model like so:

I'm using the contenttypes framework to create a "featured content" feature on my site. I've basically done this by defining a model like so:

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

What I would like to be able to do now is have a tick box on every model edit/create page from within my admin area, which when ticked and submitted adds the content reference to FeaturedContent. When unticked, likewise the reference is deleted.

If there are any better methods to do this please le开发者_开发知识库t me know. From what I can see, using contenttypes is the way to go.

Many thanks!


You will need to create a stackedinline admin, for each of your models that need this option in the admin.

Something like the following:

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

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

But this will give the inlines in their default widget, so you will also need to define a form to customize to the checkbox widget.

0

精彩评论

暂无评论...
验证码 换一张
取 消