21๐
This Snippet gives you the code for a manage.py reset_db
command you can install, itโs the minimum change that solves your problem
That said, from comments below:
You might as well just install Django command extensions to get
reset_db and other goodies:
https://github.com/django-extensions/django-extensions
1๐
You want sqlreset
:
% python manage.py help sqlreset
Usage: manage.py sqlreset [options] <appname appname ...>
Prints the DROP TABLE SQL, then the CREATE TABLE SQL, for the given app name(s).
Options:
-v VERBOSITY, --verbosity=VERBOSITY
Verbosity level; 0=minimal output, 1=normal output,
2=all output
--settings=SETTINGS The Python path to a settings module, e.g.
"myproject.settings.main". If this isn't provided, the
DJANGO_SETTINGS_MODULE environment variable will be
used.
--pythonpath=PYTHONPATH
A directory to add to the Python path, e.g.
"/home/djangoprojects/myproject".
--traceback Print traceback on exception
--version show program's version number and exit
-h, --help show this help message and exit
Just like when you modify a model, Django will not automatically do this for you. It will only output the commands for you to copy and paste.
- [Django]-Error when using django.template
- [Django]-Django content-type : how do I get an object?
- [Django]-How about having a SingletonModel in Django?
0๐
Just assign a new database and drop this db from the db console. Seems to me to be the simplest.
- [Django]-How to loop over form field choices and display associated model instance fields
- [Django]-Separating form input and model validation in Django?
- [Django]-Problems extend change_form.html in django admin
0๐
Hm, maybe you lie to manage.py, pretending to make fixtures, but only to look for apps:
apps=$(python manage.py makefixture 2>&1 | egrep -v โ(^Error|^django)โ|awk -F . โ{print $2}โ|uniq); for i in $apps; do python manage.py sqlreset $i; done| grep DROP
That prints out a list of DROP TABLE statements for all apps tables of your project, excluding django tables itself. If you want to include them, remove the |^django
pattern vom egrep.
But how to feed the correct database backend? sed/awk-ing through settings.conf? Or better by utilizing a little settings.conf-reading python script itself.
- [Django]-Django.db.utils.IntegrityError: duplicate key value violates unique constraint "django_migrations_pkey"
- [Django]-Django DRF with oAuth2 using DOT (django-oauth-toolkit)
- [Django]-Folder Structure for Python Django-REST-framework and Angularjs
0๐
Assuming you are (luckily) in a Linux environment as well as:
- using MySQL and know user/password to access projectโs db
- bash, mysql, readlink are installed
- settings.py is settings.py
- dbname is the same as your django project name (see MYAPP var below)
- all the APPS you want to reset are listed in INSTALLED_APPS as strings with fully qualified module names (starting with the name of your django project)
Change your current folder to your project code where manage.py and settings.py reside. Then run the following:
MYAPP=$(basename $(dirname `readlink -m settings.py`));DJANGOAPPS=$(echo 'import settings; print " ".join([ str(app).lstrip("'${MYAPP}'.") for app in settings.INSTALLED_APPS if "'${MYAPP}'" in app ])' | python - ); python manage.py sqlreset $DJANGOAPPS | mysql -u root --password ${MYAPP}
If you understand what is going in this bash oneliner โ it should not be a problem to build upon this idea (e.g. put it to Makefile).
- [Django]-Django form fails validation on a unique field
- [Django]-A field with precision 10, scale 2 must round to an absolute value less than 10^8
- [Django]-How to access request body when using Django Rest Framework and avoid getting RawPostDataException
0๐
In Django 3.1.1
if you want to totally reset the database and the schema then put this code in any app like this
app/management/commands/resetdb.py
import os
import glob
import shutil
from django.conf import settings
from django.core.management.base import BaseCommand
from django.db import connection
class Command(BaseCommand):
help = 'Resets the database'
def handle(self, *args, **options):
dbname = settings.DATABASES["default"]["NAME"]
with connection.cursor() as cursor:
cursor.execute("DROP DATABASE %s" % dbname)
cursor.execute("CREATE DATABASE %s" % dbname)
base = str(settings.BASE_DIR)
migrations = glob.glob(os.path.join(base, "*", "migrations"))
for migration in migrations:
shutil.rmtree(migration)
apps = [migration.split("\\")[-2] for migration in migrations]
for app in apps:
os.system("python manage.py makemigrations %s" % app)
os.system("python manage.py migrate")
now reset it using :
python manage.py resetdb
this will
- drop the database
- delete the migrations folders
- make migrations
- create a new db
- migrate the fresh tables
- [Django]-Django: Error: You don't have permission to access that port
- [Django]-Problems extend change_form.html in django admin
- [Django]-Django.db.utils.ProgrammingError: relation "bot_trade" does not exist
-2๐
take a look at reset command in djangoโs code, and write your own which drops/creates DB first.
- [Django]-How to run celery as a daemon in production?
- [Django]-Django celery task: Newly created model DoesNotExist
- [Django]-How to add new languages into Django? My language "Uyghur" or "Uighur" is not supported in Django