问题
I have a model like this:
class Person(models.Model,Subject):
name = ..
The class Subject is not supposed to be in the Database so, it doesn't extends from models.Model:
class Subject:
def __init__(self,**kargs):
_observers = []
my problem is that the constructor of Subject is never called, so i've tried adding this to the class Person:
def __init__(self):
super(Person,self).__init__()
but now i have an error saying that init takes 1 arguments but 7 are given, and the only thing i'm doing is
>>> Person.objects.get(pk=1)
now i'm lost =S do you have any idea how the constructor of person should be?
BTW: i'm using django 1.1 and python 2.6
回答1:
First of all, use new-style classes (ones that inherit from object
). Second, read about how python's super behaves in multiple inheritance scenarios: http://fuhm.net/super-harmful/
There is also a nice talk covering it: http://europythonvideos.blip.tv/file/4000758/
回答2:
You can use Django's post_init
signal. It's invoked after the model is instantiated, and is passed the instance that was created.
来源:https://stackoverflow.com/questions/3699713/multiple-inheritance-in-django-problem-with-constructors