15

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?

Geoffrey R.
  • 1,907
  • 1
  • 19
  • 32
  • 3
    Have a look at [this](http://stackoverflow.com/questions/6645051/there-is-a-way-to-add-features-to-an-existing-django-command) and [this](http://stackoverflow.com/questions/15348223/django-create-a-management-command-that-will-override-the-default-settings-in) – Moses Koledoye Jun 10 '16 at 18:18

1 Answers1

20

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

Qsebas
  • 458
  • 3
  • 15
Geoffrey R.
  • 1,907
  • 1
  • 19
  • 32