Created
February 28, 2012 17:35
-
-
Save respondcreate/1933871 to your computer and use it in GitHub Desktop.
Polymorphic Scrape Method Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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