How to delete all existing record in django model using terminal
Using Django shell:
- Open a terminal.
- Navigate to your Django project directory.
- Run the Django shell.
Execute the following commands in the Django shell to delete all records from a specific model:python manage.py shell
python
# Import the model
from your_app.models import YourModel
# Delete all records
YourModel.objects.all().delete()
Make sure to replace YourModel
with the name of the model you want to delete records from and your_app
with the name of your Django app where the model is defined.
Example:
Let's say you have a model named Employee
in an app named company
:
python
# company/models.py
from django.db import models
class Employee(models.Model):
name = models.CharField(max_length=100)
department = models.CharField(max_length=100)
# Other fields...
To delete all records from the Employee
model:
python
from company.models import Employee
Employee.objects.all().delete()
Comments
Post a Comment