Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override existing django-admin command

Tags:

python

django

In addition to writing custom django-admin commands, which is pretty well documented, I would like to be able to override an existing command, like manage.py loaddata (fixture) so I could add some further treatment after that fixtures would have been loaded into my database.

I guess I would have to write a custom command that first calls 'loaddata' and then does its own treatment. Is there a neat way to do things so?

Is there a better solution?

like image 819
Geoffrey R. Avatar asked Jun 10 '16 18:06

Geoffrey R.


1 Answers

Thanks to Moses link to other SO answers I eventually managed to write a template for additional treatment to a loaddata command. Here is a snippet which does the trick:

"""
Additional treatment for the loaddata command.
Location example: project/app/management/commands/loaddata.py
"""
from django.core.management.base import BaseCommand, CommandError
from django.core.management.commands import loaddata


class Command(loaddata.Command):

    def handle(self, *args, **options):
        super(Command, self).handle(*args, **options)
        self.stdout.write("Here is a further treatment! :)")

don't forget to put your application on top in the INSTALLED_APPS configuration

like image 91
Geoffrey R. Avatar answered Sep 28 '22 10:09

Geoffrey R.