You won't be able to use a GenericForeignKey in this situation, but
you could write a helper method and define a property to affect
something similar. For example:
class ModelTwo(models):
model_one = models.ForeignKey(ModelOne)
object_id = models.PositiveIntegerField()
def get_content_object(self):
return self.model_one.content_type.get_object_for_this_type
(id=self.object_id)
content_object = property(get_content_object)
The difference between this and a GenericForeignKey is that you will
not be able to create ModelTwo objects and specify a content_object in
the constructor. Ie. none of this:
obj = ModelTwo(content_object=some_object)
On Nov 5, 11:25 am, bruno desthuilliers
<[email protected]> wrote:
> I have this pattern:
>
> class ModelOne(models):
> content_type = models.ForeignKey(ContentType)
>
> class ModelTwo(models):
> model_one = models.ForeignKey(ModelOne)
> object_id = models.PositiveIntegerField()
>
> And I'd like to have a 'content_object' GenericForeignKey (or anything
> with a close enough behaviour) in ModelTwo. From a quick glance at the
> implementation of GenericForeignKey, it looks that 1/ it won't just
>
> So the question, obviously, is "did anyone here had the same use case,
> and if yes how did you solve it" ? FWIW, I googled for
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---