1

I'm often experimenting around creating different models, changing relations and so forth. This usually happens when starting a new project. At this phase I do not want to create any migrations but instead just get the thing up and running. So i very often do this:

rm db.sqlite3
rm -r project/apps/app/migrations/*   
python manage.py makemigrations app
python manage.py migrate app   
python manage.py createsuperuser
bla
bla

Is there any way to have this "reset" function more quickly? I frustratingly found out, that django does not allow superusers to be created by a shell script.

Is there any way to purge the db without removing the users? How do you do this?

JasonTS
  • 2,479
  • 4
  • 32
  • 48
  • 1
    Save your superuser to a fixture `python manage.py dumpdata auth.User -o superuser.json` and then you can just run `python manage.py loaddata superuser.json`? BTW it is possible to create a superuser via shell script, you have to pass `--noinput` and set an env var but it's possible – Iain Shelvington May 21 '22 at 13:50
  • 1
    Create a script with all the commands you run and just keep running that script? – Abdul Aziz Barkat May 21 '22 at 15:00
  • Oh, yes. My knowledge seems to be outdated. Since Django 3.0 it should be possible again. Just found this: https://stackoverflow.com/questions/32532900/not-able-to-create-super-user-with-django-manage-py – JasonTS May 21 '22 at 15:18

1 Answers1

0

Try this:

Reset the Whole Database in Django

python manage.py flush

Reset an App Database Tables in Django

python manage.py migrate MyApp zero

Ramlakhan Kevat
  • 317
  • 1
  • 7
  • 1
    This seems promising, but I get stuck to the same issues again. Flush deletes the users which is annoying and migrating to zero just moves back up the chain. The unused migrations are still there, the db is suddenly empty. – JasonTS May 21 '22 at 14:55
  • delete all migration file manual – Ramlakhan Kevat May 21 '22 at 15:18