Tuesday, 15 March 2011

django - Changing save or put functions in database orms -


While working with the database I have noticed something, especially Python ORM which deal with the database.

Here is an example of the save () function in Dijongo:

  def save (self, ** kwargs): self.somevar = Dosomething (self.somethervar) is back super (, self) .save ()   

Also in the Google app engine, where the function is inserted () is used:

  def put (self, ** kwargs): self. & Lt; Some_var & gt; = DoSomething (self.someOtherVar) returns super (, self) .save ()   

Now, I think why we do this, IIRC, we Do this because we are not manually using the put () or save function, so we use super call to make it work properly Will happen.

Anyone please give me a more detailed explanation about why to save the work in such a way. I understand this is my mistake, because I had learned through going and coding, but I actually I can not understand that when I type save () or ) function

You are currently viewing a method that overrides in a sub-class. The purpose of subclassing is to reuse the code and override functionality as needed, sometimes it involves overriding the method and calling its parents' pieces.

Super calls the method of the original class

This is not exactly what is subclassing:

  Def Save (...): Return Super (MyClass, self) .ave (...)   

On the contrary, if we write a method without a super call It is

  druf (...): print ("I have nothing with parent class") < / Code>  

If we want to supplement the functionality (i.e. retaining the old behavior but something that The Hen extra) We use super to call from within the new method the original pattern.

  def Save (...): Print ("I'm directly told before the original root method") Returns Super (MyCalus, Self) Savings (...)    

No comments:

Post a Comment