Start django shell with a temporary database
Asked Answered
F

2

12

I want to fire up the django shell with a temporary database (like what's done when doing django tests)

Is there any command like:

python manage.py testshell

where I can create a bunch of bogus models without polluting my database?

Freemon answered 1/6, 2014 at 1:57 Comment(1)
IIRC, the unit testing framework creates the test DB for you. You just have to make sure it has the right permissions to do so.Aestival
F
22

Nevermind, this blog post explains it

>>> from django import test
>>> test.utils.setup_test_environment() # Setup the environment
>>> from django.db import connection
>>> db = connection.creation.create_test_db() # Create the test db
Freemon answered 1/6, 2014 at 2:6 Comment(5)
Even though you answered it yourself, mark this answer as correct so this doesn't show up as an unanswered question.Aestival
@MikeDeSimone I have 2 more days until I can do that, but will do.Freemon
Ah, forgot about the delay.Aestival
The blog link is invalid, but I think the commands are adequate for a test shell. Maybe the two commands connection.creation.destroy_test_db() and test.utils.tear_down_test_environment() are also helpful.Surpassing
Use db = connection.creation.create_test_db(keepdb=True) if you want to connect to an existing database and you're using Django >= 1.8. Docs are here.Decentralization
F
-2

You could just turn autocommit off:

from django.db import transaction
transaction.set_autocommit(False)
Fenrir answered 22/1, 2015 at 0:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.