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
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.