views:

34

answers:

1

Hi,

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 let me know. From what I can see, using contenttypes is the way to go.

Many thanks!

A: 

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.

Lakshman Prasad
If I use admin.StackedInLine it just whines about not having a foreign key.'mySite.testing.models.FeaturedContent'> has no ForeignKey to 'mySite.testing.models.MyContent'If I use "generic.GenericTabularInline" instead of "admin.StackedInline" it at least loads me an empty table. I'm really confused so far :(
Crungmungus