Django manage.py command
Create 2 nested packages in your application:
management
commands
inside ofmanagement
And create py
file with a name you want for your command.
Example for resave
command in main
application :
In resave.py
file create Command
class with wanted code in handle
method:
from django.core.management.base import BaseCommand, CommandError
from main.models import Tip
class Command(BaseCommand):
help = 'Resaves tips'
def handle(self, *args, **options):
# your code here
for t in Tip.objects.all():
self.stdout.write("Resaving tip {}".format(t))
t.save()
Now you can call
python manage.py resave