Geoffrey R.
Geoffrey R.

Reputation: 2116

Override existing django-admin command

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?

Upvotes: 16

Views: 4909

Answers (1)

Geoffrey R.
Geoffrey R.

Reputation: 2116

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

Upvotes: 24

Related Questions