I need a BaseCommand that deletes entries from the specified model in the parameters.
Run this:
./manage.py delete_data_model app.shop
# delete_data_model.py
from django.core.management.base import BaseCommand, CommandError
from django.db.transaction import atomic
class Command(BaseCommand):
help = "Deleted data from model"
def add_arguments(self, parser):
parser.add_argument('model', required=True, type=str)
def handle(self, *args, **options):
self.stdout.write("Begin")
with atomic(using='default'):
try:
path = options['model']
app, model = path.split('.')[:2]
from (app) import (model) as Model # ??? how do this
Model.objects.all().delete()
except Exception as e:
raise CommandError("Error {}".format(e))
self.stdout.write("Complete")
I expect empty table Shop.