How to remove all of the data in a table using Django

后端 未结 8 1768
执笔经年
执笔经年 2020-12-12 21:12

I have two questions:

  1. How do I delete a table in Django?
  2. How do I remove all the data in the table?

This is my code, which is not succe

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-12 21:52

    You can use the Django-Truncate library to delete all data of a table without destroying the table structure.

    Example:

    1. First, install django-turncate using your terminal/command line:
    pip install django-truncate
    
    1. Add "django_truncate" to your INSTALLED_APPS in the settings.py file:
    INSTALLED_APPS = [
        ...
        'django_truncate',
    ]
    
    1. Use this command in your terminal to delete all data of the table from the app.
    python manage.py truncate --apps app_name --models table_name
    

提交回复
热议问题