Setting the choices attribute as Django complains
must be a "ContentType" instance
ContentType.objects.get() returns an instance of a particular model, not
ContentType (as one would expect), so I'm not sure how to create an
instance with the particulars I need to point to something specific in
the choices list. Here's what I have:
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
class GenericFKExample(models.Model):
CHOICES = (
(ContentType.objects.get(model="model1"), "Model 1"),
(ContentType.objects.get(model="model2"), "Model 2"),
)
content_type = models.ForeignKey(ContentType,
choices = CHOICES,
null = True,
)
object_id = models.PositiveIntegerField(
null = True,
)
content_object = generic.GenericForeignKey(
"content_type", "object_id"
)
Obviously, if I don't use choices, it works but then I have a very long
list of model choices.
--
Adam Stein @ Xerox Corporation Email: ad...@eng.mc.xerox.com
Disclaimer: Any/All views expressed
here have been proven to be my own. [http://www.csh.rit.edu/~adam/]