Skip to content

Instantly share code, notes, and snippets.

@easydevmixin
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save easydevmixin/b0a99adcdb82790bc72e to your computer and use it in GitHub Desktop.
Save easydevmixin/b0a99adcdb82790bc72e to your computer and use it in GitHub Desktop.
Creating a Django 1.8 admin command
def clear_whole_cache():
print("Hey, the whole cache is cleared!")
def clear_partial_cache():
print("Hey, partial cache is cleared!")
from django.core.management.base import BaseCommand, CommandError
from example import cacheutils
class Command(BaseCommand):
def add_arguments(self, parser):
# This is an optional argument
parser.add_argument(
'--partial', # argument flag
action='store_true', # action to take, stores true if present
dest='partial', # argument name
default=False, # it is false by default
help="Clears the cache partially" # a help message
)
def handle(self, *args, **options):
try:
if options['partial']:
cacheutils.clear_partial_cache()
else:
cacheutils.clear_whole_cache()
except Exception as e:
raise CommandError("An error has occurred: {}".format(e))
self.stdout.write("Command executed successfully")
example/
└── management
│ ├── commands
│ │ ├── clearcache.py
│ │ └── __init__.py
│ └── __init__.py
├── cacheutils.py
├── ...
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment