简体   繁体   中英

Multiple inheritance in django. Problem with constructors

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

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/

You can use Django's post_init signal . It's invoked after the model is instantiated, and is passed the instance that was created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM