Last active
August 29, 2015 14:24
-
-
Save easydevmixin/b0a99adcdb82790bc72e to your computer and use it in GitHub Desktop.
Creating a Django 1.8 admin command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def clear_whole_cache(): | |
print("Hey, the whole cache is cleared!") | |
def clear_partial_cache(): | |
print("Hey, partial cache is cleared!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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