Multiple inheritance in django. Problem with constructors

此生再无相见时 提交于 2019-12-24 16:14:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!