I'm writing custom django commands under my apps management/commands
directory. At the moment I have 6 different files in that directory. Each file has a different command that solves a unique need. However, there are some utilities that are common to all of them. What is the best way to abstract away this common code?
Below is an example:
load_colors
class Command(BaseCommand):
def handle(self, *args, **options)
....code unique to colors
def check_validity(self, color)
....#code common to both shades and colors
load_shades
class Command(BaseCommand):
def handle(self, *args, **options)
....#code unique to shades
def check_validity(self, color)
....#code common to both shades and colors
In the management/commands folder create the file _private.py:
#_private.py
from django.core.management.base import BaseCommand, CommandError
class SharedCommand(BaseCommand):
def check_validity(self, color):
...
The in other files import this file and inherit SharedCommand:
# command01.py
from ._private import *
class Command(SharedCommand):
def handle(self, *args, **options):
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With