Skip to content

Instantly share code, notes, and snippets.

@respondcreate
Created February 28, 2012 17:35
Show Gist options
  • Save respondcreate/1933871 to your computer and use it in GitHub Desktop.
Save respondcreate/1933871 to your computer and use it in GitHub Desktop.
Polymorphic Scrape Method Example
from django.db import models
from django.contrib.contenttypes.models import ContentType
from polymorphic.models import PolymorphicLink
class SomeClass(models.Model):
"""Some attributes"""
def _get_features(self):
ct = ContentType.objects.get(app_label=self._meta.app_label, name=self._meta.verbose_name)
features_set = PolymorphicLink.objects.filter(parent_content_type=ct, parent_object_id=self.pk)
features = []
for feature in features_set:
this_ct = ContentType.objects.get(app_label=feature.content_type.app_label, model=feature.content_type.model)
thing = this_ct.get_object_for_this_type(pk=feature.object_id)
features.append(thing)
return features
features = property(_get_features)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment