On Fri, Jul 08, 2011 at 09:02:06AM -0700, gontran wrote: > Hello everyone, > > > my models are defined like this: > > class MyBaseModel(models.Model): > ..... > class Meta: > abstract = True > > class ClassA(MyBaseModel): > ...... > > class ClassB(MyBaseModel): > ...... > > Then I'm trying to get a subclass of MyBaseModel by doing this: > > for s in MyBaseModel.__subclasses__(): > if s.__name__ == "ClassA": > MyClass = s > elif s.__name__ == "ClassB": > MyClass = s > q = MyClass.objects.all() ===> Raise an AttributeError: type object > 'ClassA' has no attribute '_default_manager' > > I don't understand why it raises this error, what am I missing?
I'd say (though I may be absolutely wrong) that this is caused by the fact that model classes are processed by the ModelBase metaclass. My guess is (and it is really just a guess, since I can't find anything more about __subclasses__ than that it exists [1]) that __subclasses__ returns a reference to the original class as defined in the Python code, before it is processed by the metaclass. Looking at one example abstract model class I have in my own project, this appears to be the case. In the class returned by __subclasses__() of the abstract class I see only things defined in models.Model and in the abstract class, but none of the fancy stuff (like model fields or managers, i. e. the stuff that is processed by the metaclass) I defined in the concrete model. Michal [1] http://docs.python.org/library/stdtypes.html#class.__subclasses__
signature.asc
Description: Digital signature

