class ContentTypeToGetModel(object): """ requires fields: - content_type: FK(ContentType) - object_id: PositiveIntegerField() """ def get_related_object(self): """ return the related object of content_type. eg: """ # This should return an error: MultipleObjectsReturned # return self.content_type.get_object_for_this_type() # So, i handle it with this one: model_class = self.content_type.model_class() return model_class.objects.get(id=self.object_id) @property def _model_name(self): """ return lowercase of model name. eg: `question`, `answer` """ return self.get_related_object()._meta.model_name """ # USAGE EXAMPLE class Flag(TimeStampedModel, ContentTypeToGetModel): creator = models.ForeignKey( User, related_name='flag_creator') content_type = models.ForeignKey( ContentType, related_name='flags', on_delete=models.CASCADE) object_id = models.PositiveIntegerField(_('Object id')) content_object = GenericForeignKey('content_type', 'object_id') """