views:

21

answers:

1

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

+1  A: 

If you don't mind doing the lookups yourself then you don't even need GenericForeignKey. Simply make it a property that does the lookup from the appropriate fields and returns the model. You'll lose any reverse functionality unless you do more work, but most of the time that isn't what is cared about.

Ignacio Vazquez-Abrams
Does this imply that what I'm trying to do is not possible?
slypete
With Django's current `contenttypes` framework, probably. I'm sure that it *could* be done if someone cared enough to implement it.
Ignacio Vazquez-Abrams
I guess my question was "is it implemented?" Apparently not... Thanks
slypete
Is there any other way to use the built-in GenericForeignKey with the content_type stored elsewhere?
slypete
Not as far as I know.
Ignacio Vazquez-Abrams