0

I'm trying to implement for methods for eac Model object in my project. get_all() , get_by_id(),add(),remove() and maybe others. I need to use them by each object . When I declare an object automatically can call one of those methods. The problem , I don't want to duplicate the code of the four methods in each class . Is there a way to write them once, and link them to each object. I heared about instance methods in python. How can use this. Please a help Thanks ...

3 Answers 3

3

You can have a base manager which has all these methods and inherit them. Django model and manager inheritance

Sign up to request clarification or add additional context in comments.

Comments

0
class MyModel(models.Model):
    def get_all():
        return <something>

class A(MyModel):
    pass

class B(MyModel):
    pass

Comments

0

You really should seperate row-level actions (like delete(), get_some_calculated_attribute()) which go in Model classes from table level actions (like create(), filter()) which go in Manager classes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.