Call CURD operation of a class from another class

Question:

I have a class with create, update, and delete functions of Django CRUD
Can I use the create, and update functions of first in another class create function

from example :

class A(viewsets.ModelViewSet):

     def create(self, request, *args, **kwargs):
     some code 
     
     def update(self, request, *args, **kwargs): 
     some code

Now as you can see the first class A have create and update function
Now I have another class B which also a have create function
now how can use call the create function of first class A

class B(viewsets.ModelViewSet):

     def create(self, request, *args, **kwargs):
     some code 
     
Asked By: Adarsh Srivastav

||

Answers:

First of, I agree with Yussef’s answer. It should be working.

An alterntive would be inheritance:

class B(A):  # let B inherit from A
    def create(self, request, *args, **kwargs):
        print("here your logic")
        super().create(request, *args, **kwargs)  # call the function of parent class
        print("some more logic")

Sure, inheritance brings along more advantages, but those might also be disadvantage if it does not suit your needs. Because by inheriting from A, your class B now also has the same update function as A. Normal inheritance behaviour

Answered By: Tarquinius

You can directly call the other class from the class you want
just use .create function and pass the required param

Answered By: Adarsh Srivastav
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.