You're close...
change your method in CHOICES to get_for_model( _model_ )
So it should be
CHOICES = (
(ContentType.objects.get_for_model(My_Model), "Model 1"),
(ContentType.objects.get_for_model(My_Other_Model), "Model
2"),
)
Obviously, make sure you import your model before you try to use it.
On Mar 23, 3:26 pm, Adam Stein <[email protected]> wrote:
> Trying to create a generic FK using ContentType. In admin, the menu
> lists all the models. Since I only ever need to select 1 of 2 different
> models, anyway to limit the choice?
>
> 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: [email protected]
>
> Disclaimer: Any/All views expressed
> here have been proven to be my own. [http://www.csh.rit.edu/~adam/]
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---