Issue #6 resolved

InheritanceManager strange behavior with django 1.3-beta1

Mateusz Łapsa-Malawski
created an issue

select_subclasses with specific subclasses provided as argument works perfect!

Unfortunately autodetection of subclasses is a bit problematic.

{{{

!python

Node.objects.all().select_subclasses()

ERROR: An unexpected error occurred while tokenizing input The following traceback may be corrupted or invalid The error message is: ('EOF in multi-line statement', (41, 0))


AttributeError Traceback (most recent call last)

/Users/munhitsu/workspace-aptana/cr3components/<ipython console> in <module>()

/Users/munhitsu/workspace-aptana/cr3components/parts/django-model-utils/model_utils/managers.pyc in select_subclasses(self, subclasses) 11 if not subclasses: 12 subclasses = [o for o in dir(self.model) ---> 13 if isinstance(getattr(self.model, o), SingleRelatedObjectDescriptor) 14 and issubclass(getattr(self.model,o).related.model, self.model)] 15 new_qs = self.select_related(subclasses)

/Users/munhitsu/workspace-aptana/cr3components/parts/django/django/db/models/fields/subclassing.pyc in get(self, obj, type) 93 def get(self, obj, type=None): 94 if obj is None: ---> 95 raise AttributeError('Can only be accessed via an instance.') 96 return obj.dict[self.field.name] 97

AttributeError: Can only be accessed via an instance. }}}

Comments (3)

  1. Rafał Stożek

    It's caused by fields that are not related (like DateField, TextField etc). Here's a patch to InheritanceQuerySet which uses model's meta instead of dir(), so it iterates only on related objects:

        def select_subclasses(self, *subclasses):
            m = self.model
            if not subclasses:
                subclasses = [r.get_accessor_name() for r in m._meta.get_all_related_objects() if
                              isinstance(getattr(m, r.get_accessor_name()), SingleRelatedObjectDescriptor)
                              and issubclass(r.model, m)]
            new_qs = self.select_related(*subclasses)
            new_qs.subclasses = subclasses
            return new_qs
    

    It's tested under django 1.3-svn. It should work under django 1.2.x but it needs tests.

  2. Rafał Stożek

    Well, it seems that error occurs, when there are other fields which use descriptors on model (like FileField for example). I sent you pull request. My changes are tested under django 1.3-rc1, django 1.2.5 and django 1.2.4.

  3. Log in to comment