I am trying to delete records in real time or automatically similar to this
As i have understand (I am a newbie) you need write a management command but I am having a hard time grasping the concept and how can it be in real time? Can someone point me in the right direction? I am very confused. Thank you in advance.
Everything you want to know about custom Django commands can be found in the docs: https://docs.djangoproject.com/en/1.8/howto/custom-management-commands/
Basically, you need to create a management/commands
directory, so your folder structure looks like this:
app/
... (some stuff you're having in your app directory)
management/
__init__.py
commands/
__init__.py
delete_all.py
Now in delete_all.py
you want to declare a new command class extending BaseCommand
and responsible for all your logic. The easiest implementation of such will look like this:
# delete_all.py
from django.core.management.base import BaseCommand
from app.models import MyModel
class Command(BaseCommand):
def handle(self, *args, **options):
print('Deleting all MyModel records!')
MyModel.objects.all().delete()
print('Successfully deleted all MyModel records!')
Now you can access your command with manage.py
:
python manage.py delete_all