How to delete all existing record in django model using terminal

 

Using Django shell:

  1. Open a terminal.
  2. Navigate to your Django project directory.
  3. Run the Django shell.


python manage.py shell
Execute the following commands in the Django shell to delete all records from a specific model:
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

Popular posts from this blog

Basic Web Design with HTML and CSS

Project Web Design Assignment