views:

34

answers:

1

Hi,

Sent a question yesterday but I wasn't too clear on what I am attempting. I'm struggling to find any useful tutorials on how best to achieve this so a nudge in the right direction would be greatly appreciated.

I've set up a model which references django_content_type. My idea is to use this model to reference other models so that they can be marked as "featured" content, without me having to put a new "isFeatured" field on every model.

All I want to be able to do is have a check box appear on the forms of my admin for each model, which indicates if the content is featured or not.

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

Firstly, is this the right approach?

Do I need to create a custom form/model field? and how do I introduce this field to the admin template?

I've been looking inside of (django.contrib.contenttypes) generic.py and you can clearly see where the template data is being loaded and also which template is being used. I'm wondering if perhaps I need to extend this class rather than write my own field?

Thanks for any help,

James

A: 

In my view this isn't the right approach. You're better off using abstract base classes with a BooleanField, like this:

class FeaturedModel(models.Model):
    is_featured = models.BooleanField(default = False)

    class Meta:
        abstract = True

class Foobar(FeaturedModel):
    title = models.CharField(max_length = 200)
    ...

That way you get all the goodness of each model having it's very own is_featured field that you can play with, and none of the complication of content types.

Dominic Rodger
That's a really awesome idea Dominic, thank you :)
Crungmungus