0

I have model

Order(models.Model):
  name = models.Charfield()
  
  @classmethod 
  do_something(cls):
    print('do soemthing')

What I want to do is to move do_something method from my model to another file.I want to do it because I have several other big methods in this model and want to structure the code, don't like lengh of this file. It's getting big > 700 lines of code.

So I want to move my method to another file and import it, so it still can be used like modelmethod

like this:

Order.do_something()

Any ideas?

user2950593
  • 9,233
  • 15
  • 67
  • 131
  • Try creating a new class with `do_something` method and inherit it in `Order` class. – Harun Yilmaz Aug 27 '20 at 12:17
  • 1
    Does this answer your question? [Python: How can I separate functions of class into multiple files?](https://stackoverflow.com/questions/47561840/python-how-can-i-separate-functions-of-class-into-multiple-files) – Harun Yilmaz Aug 27 '20 at 12:20

1 Answers1

1

Use inheritance -- (wiki)

# some_package/some_module.py

class MyFooKlass:
    @classmethod
    def do_something(cls):
        # do something
        return 'foo'

# my_app/models.py

from some_package.some_module import MyFooKlass

class Order(models.Model, MyFooKlass):
    name = models.CharField()
JPG
  • 82,442
  • 19
  • 127
  • 206