Skip to content

Instantly share code, notes, and snippets.

@SmileyChris
Created December 5, 2010 22:04
Show Gist options
  • Save SmileyChris/729525 to your computer and use it in GitHub Desktop.
Save SmileyChris/729525 to your computer and use it in GitHub Desktop.
class YourBaseModel(models.Model):
@property
def child(self):
from django.core.exceptions import ObjectDoesNotExist
for related_object in self._meta.get_all_related_objects():
if not issubclass(related_object.model, self.__class__):
continue
try:
return getattr(self, related_object.get_accessor_name())
except ObjectDoesNotExist:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment