Storing GenericForeignKey content_type in another model?

Posted by slypete on Stack Overflow See other posts from Stack Overflow or by slypete
Published on 2010-04-07T04:47:58Z Indexed on 2010/04/07 4:53 UTC
Read the original article Hit count: 273

Filed under:

I have a typical definition/instance situation in my data modeling. I'm trying to store the content_type of a GenericForeignKey in another model (the definition model) like so:

class IndicatorFieldInstance(models.Model):
    definition = models.ForeignKey(IndicatorField)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey(definition.content_type, 'object_id')
    indicator_instance = models.ForeignKey(IndicatorInstance)

I've also tried it like this:

content_object = generic.GenericForeignKey('definition__content_type', 'object_id')

Neither of these methods seem to work. Is it possible to achieve this?

For reference, here's the definition model:

class IndicatorField(models.Model):
    name = models.CharField(max_length='255')
    content_type = models.ForeignKey(ContentType)
    indicator = models.ForeignKey(Indicator)

Thanks, Pete

© Stack Overflow or respective owner

Related posts about django-models