0

I'm a Django and DB noob, but I'm working on a website that has both blogs and articles. They are instantiated in admin in their own respective fields and basically I have a class "FeaturedPost" in model.py where for one of the attributes "content" I want to be able to pick from the available blogs or articles.

I'm aware that if I want to map the content to a blog, I would do a

models.ForeignKey(Blogs, related_name="w/e")

but how do I abstract this so I can pick from the two content types? Would a GenericForeignKey be helpful?

I'm using Fein-CMS if that helps with anything in this case.

karthikr
  • 97,368
  • 26
  • 197
  • 188
Ned U
  • 401
  • 2
  • 7
  • 15

1 Answers1

0

Correct, GenericForeignKey is what you need. i.e.

from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType

class FeaturedPost(models.Model):
    ...
    content_type = models.ForeignKey(ContentType)
    content_object_id = models.PositiveIntegerField()
    content = GenericForeignKey('content_type', 'content_object_id')

To edit these in the admin, you'll need to use a generic inline

Greg
  • 9,963
  • 5
  • 43
  • 46
  • Thank you! This will help with what I ultimately need. Now I have to get the actual data from inputs in admin and make it do things. – Ned U Jun 17 '14 at 23:44