我在Django中有以下代码:
class Parent(models.Model):
def save(self):
# Do Stuff A
class Mixin(object):
def save(self):
# Do Stuff B
class A(Parent,Mixin):
def save(self):
super(A,self).save()
# Do stuff C
现在,我想使用mixin而不在父级中剔除保存的行为.因此,当我保存时,我想要做C,B和A的东西.我读过Calling the setter of a super class in a mixin然而我没有得到它并且阅读了超级文档它似乎没有回答我的问题.
问题是,我应该在mixin中加入什么来确保它能够完成B并且不会阻止Stuff A发生?
解决方法
如何在你的mixin课程中调用super?
class Parent(object):
def test(self):
print("parent")
class MyMixin(object):
def test(self):
super(MyMixin,self).test()
print("mixin")
class MyClass(MyMixin,Parent):
def test(self):
super(MyClass,self).test()
print("self")
if __name__ == "__main__":
my_obj = MyClass()
my_obj.test()
这将为您提供输出:
$python test.py parent mixin self