[Answered ]-Django CBV: How to subclass scattered parts of code from the parent class

0👍

As mentioned by @lhasaDad above, just need to call the methods in order.
Note they can be written outside the Post() method, but need to be called in the Post() method!

enter image description here

👤Matt S

1👍

When you are dealing with classes and inheritance you are dealing with items that hold state (attributes) and functions that manipulate that state and query it (methods). A class is a ‘template’ of what an instance of that class should have as far as attributes and function.

You instantiate the class (in python by calling the Class name as a function) and passing in any data the class will need. This is presented to the init method as arguments. you also get a reference to the instance in the self variable (first argument of instance methods). You can use the self variable to save the values passed in with the instance. Your Class name invocation will return a reference to the instance of the class. Programs use returned value to call the methods on your instance (example: myinstance.mymethod() ). This will decide the order of execution of the functions for the instance of your class. Each function called will be passed the self variable which allows you to refer again to the values you saved as attributes for the instance (like in the init method).

In your Django framework you seem to be using, the CBV framework defines when certain methods will be called (get, post, etc). once they are active if you need to call another method in the class you can do a

self.first_method_to_run()
self.second_method_to_run()

within the get/post/whatever method. this will run them in that order and will look for the method (first in the child class and run it starting there and then in the parent class and run it from there) in the class hierarchy.

Hope this helps clear some things up.

Leave a comment