diff --git a/CHANGES.rst b/CHANGES.rst index 9b3c339..453dc48 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -7,6 +7,9 @@ tip (unreleased) 0.6.0 (2011.02.18) ------------------ +- Fixed issue #6, bug with InheritanceManager and descriptor fields (e.g. + FileField). Thanks zyegfryed for the fix and sayane for tests. + - updated SplitField to define get_prep_value rather than get_db_prep_value. This avoids deprecation warnings on Django trunk/1.3, but makes SplitField incompatible with Django versions prior to 1.2. diff --git a/model_utils/managers.py b/model_utils/managers.py index 7b764dd..8501e45 100644 --- a/model_utils/managers.py +++ b/model_utils/managers.py @@ -2,16 +2,16 @@ from types import ClassType from django.contrib.contenttypes.models import ContentType from django.db import models -from django.db.models.fields.related import SingleRelatedObjectDescriptor +from django.db.models.fields.related import OneToOneField from django.db.models.manager import Manager from django.db.models.query import QuerySet class InheritanceQuerySet(QuerySet): def select_subclasses(self, *subclasses): if not subclasses: - subclasses = [o for o in dir(self.model) - if isinstance(getattr(self.model, o), SingleRelatedObjectDescriptor) - and issubclass(getattr(self.model,o).related.model, self.model)] + subclasses = [rel.var_name for rel in self.model._meta.get_all_related_objects() + if isinstance(rel.field, OneToOneField) + and issubclass(rel.field.model, self.model)] new_qs = self.select_related(*subclasses) new_qs.subclasses = subclasses return new_qs diff --git a/model_utils/tests/models.py b/model_utils/tests/models.py index 5e63597..4d54369 100644 --- a/model_utils/tests/models.py +++ b/model_utils/tests/models.py @@ -1,3 +1,4 @@ +from datetime import datetime from django.db import models from django.utils.translation import ugettext_lazy as _ @@ -7,21 +8,35 @@ from model_utils.fields import SplitField, MonitorField from model_utils import Choices class InheritParent(InheritanceCastModel): + non_related_field_using_descriptor = models.FileField(upload_to='test') + normal_field = models.TextField() pass class InheritChild(InheritParent): + non_related_field_using_descriptor_2 = models.FileField(upload_to='test') + normal_field_2 = models.TextField() pass class InheritChild2(InheritParent): + non_related_field_using_descriptor_3 = models.FileField(upload_to='test') + normal_field_3 = models.TextField() pass class InheritanceManagerTestParent(models.Model): + # test for #6 + # I'm using FileField, because it will always use descriptor + non_related_field_using_descriptor = models.FileField(upload_to='test') + normal_field = models.TextField() objects = InheritanceManager() class InheritanceManagerTestChild1(InheritanceManagerTestParent): + non_related_field_using_descriptor_2 = models.FileField(upload_to='test') + normal_field_2 = models.TextField() pass class InheritanceManagerTestChild2(InheritanceManagerTestParent): + non_related_field_using_descriptor_2 = models.FileField(upload_to='test') + normal_field_2 = models.TextField() pass class TimeStamp(TimeStampedModel):