model - Django : Does it make sense to override save method in a mixin? -
i inquire myself if create sense override save method in mixin.
in project have, reasons, override save method in several models. @ first, have create custom model class inherit models.model. semantically, doing giving role class (rather defining object in own right), that's why think it's improve write mixin. other reason because may utilize multiple inheritance in near future. on over hand, line in overriden save method :
super(mymixin, self).save(*args, **kwargs) does not create sens can used django.db.models.model class.
class mymixin(object): def save(self, *args, **kwargs): ... super(mymixin, self).save(*args, **kwargs) ... could help me decide best selection ? (mixin or custom model)
the way mro (method resolution order) works, both methods valid. abstract model case quite simple: have single inheritance chain, , each phone call super called on next class in chain. if have:
class mybasemodel(models.model): def save(self, *args, **kwargs): super().save(*args, **kwargs) class mymodel(mybasemodel): def save(self, *args, **kwargs): super().save(*args, **kwargs) the phone call in mymodel propogate mybasemodel, , phone call in mybasemodel propogate models.model.
with multiple inheritance, inheritance chain different. first class upwards in inheritance chain first base of operations class defined. if have class mymodel(mymixin, models.model), mymixin first class upwards. next, when super() called in mymixin (with mymodel instance), siblings of mymixin class. means next method called save method on models.model.
considering this, fine utilize mixin override save methods. in both cases, mymodel save method called first, mixin/abstract model's save method, , models.model save method.
note:
this simplified explanation of method resolution order works in specific case. actual algorithm determining order c3 linearization algorithm. finish explanation can found here.
django model mixins
No comments:
Post a Comment