mixin.py
from django.contrib.auth.decorators import login_required
class LoginRequiredMixin():
@classmethod
def as_view(cls):
view = super().as_view()
return login_required(view)
from utils.mixin import LoginRequiredMixin
class OrderPlaceView(LoginRequiredMixin, View):
可以看到熟丸,在LoginRequiredMixin類中并沒有as_view方法,而且它只是繼承了object,也沒有as_view方法老翘,但是它的子類中是多繼承,在View中有as_view()方法,所以LoginRequiredMixin使用了View類中的as_view方法铺峭。
- 下面在來一段簡單的代碼理接:
class Father():
def view(self):
print("this is Father's CLass")
class Mother():
def view(self):
super().view()
print("this's Mother")
class Son(Mother,Father):
def view(self):
super().view()
print('Son')
son = Son()
son.view()
輸出
this is Father's CLass
this's Mother
Son
- 創(chuàng)建Son對(duì)象墓怀,執(zhí)行view方法,方法繼承自上一級(jí)類卫键,找Mother傀履,有,進(jìn)去莉炉,發(fā)現(xiàn)沒有具體實(shí)現(xiàn)钓账,找上一級(jí)的類Father,有具體實(shí)現(xiàn),執(zhí)行Mother中的其他操作絮宁,執(zhí)行Son類中的其他操作梆暮。