Changeset 7098 brakes model class creation if one uses own django.db.model.base.Model class
|Reported by:||moep||Owned by:||mtredinnick|
|Component:||Database layer (models, ORM)||Version:||master|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Changeset 7098 brakes model class creation if one uses an own django.db.model.base.Model replacement class.
The problem is that ModelBase.__new__ assumes that a class that is a subclass of Model automatically has a _meta attribute.
That is not necessarily the case. If you use a custom replacement for Model that extends Model it is a subclass of Model but
doesn't have a meta attribute.
from django.db import modfrom django.db import models class MyModelBase(models.base.ModelBase): pass class MyModel(models.base.Model): pass class Foo(MyModel): pass
$ import mymodel >>> AttributeError: type object 'MyModel' has no attribute '_meta'
The problem is that you cannot so easily decide whether a class that extends Model is a Model Class or not. You could
of course check whether Class has a _meta attribute but that isn't a nice solution.
I think the only clean way to test if a class that extends Model is actually a Model is to use a special class from which
you can derive. But that's is why Model exists in the first place (except for metaclassing) I think.
So we need a design decision here, whether we add a new class that extends and replaces models.Model, we add a "ugly"
test if class has a _meta attribute or don't allow replacing model.Model at all.
Change History (3)
comment:1 Changed 6 years ago by moep
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset