开发者

django featured content field

开发者 https://www.devze.com 2022-12-17 23:24 出处:网络
开发者_如何学GoSent 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 wou
开发者_如何学Go

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


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.

0

精彩评论

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

关注公众号