chrisglass/django_polymorphic — pull request #67 by hottwaj (2014-02-18 18:10:34+00:00)
Title: parent_link and related_name support for inheritance fields

Adds support for user-defined fields for inheritance relations made via "parent_link" and "related_name" e.g.

class MySubClass(MySuperClass):
my_super = models.OneToOneField(MySuperClass, parent_link=True, related_name="my_sub")

Previously, the my_super field which specifies the link to the MySubClass parent (using parent_link = True), would not be picked up by django_polymorphic, which previously expected the field to the parent to be called "mysubclass_ptr".

Additionally, django_polymorphic would previously expect the reverse relation link from MySuperClass to MySubClass to be called "mysubclass", but this can be overriden by the user if they specify "related_name" in the parent_link field, as in the example above.

To enable the above, I have used model._meta.parents to determine superclasses. _meta.parents is a dict of (superclass: field_to_superclass) pairs.

By using the field name of field_to_superclass, we can work out the field to use the django_polymorphic accessor on, even if a user-specified OneToOneField to parent is is used (with parent_link=True) to get to the parent in the inheritance hierarchy.

For subclasses, the path to the subclasses can be manually specified by the user if they use a 'related_name' on the OneToOneField from the subclass to the superclass. I have changed the code to also support 'related_name' if present.