I have the following code, in 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
Now, I want to use the mixin without blatting the behaviour of the save in Parent. So I when I save, I want to do stuff C, B, and A. I’ve read Calling the setter of a super class in a mixin however I don’t get it and having read the super docs it doesn’t seem to answer my question.
THe question is, what do I put in the mixin to make sure it does stuff B and doesn’t stop Stuff A from happening?
Advertisement
Answer
How about calling super in your mixin class?
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()
This will give you the output as:
$ python test.py parent mixin self