Hi
How can I delete some records(rows) from a queryset but not from the original table in database?
I used exclude and delete for a queryset but it hit the database.
Thanks
Hey there.
The delete
method deletes all the objects that your queryset contains, this means deleting them from the database. For example, the below code will delete all rows in the table users
:
User.objects.delete()
The exclude
method it’s the reverse operation of filter
, both of them filters down the queryset for a specific criteria. Take the examples below:
User.objects.filter(is_active=True)
Will filter all the active users from the database. while in the other hand User.objects.exclude(is_active=True)
will filter for inactive users from the database.
Hey there!
The terminal will do it, directly deleting the rows from the database too.
In the terminal you can:
python manage.py shell
# import the user model
User.objects.delete()
Can you open another post for this specific question?