Using sqlite :memory: database for development.

515 views
Skip to first unread message

Thiago de Arruda

unread,
Aug 16, 2010, 10:57:31 PM8/16/10
to Django users
Hi,

I'm coming to django from grails, in which which I used an in-memory
hsql database for development and in my startup script I initialized
the in-memory database with some stub values/users that were inserted
everytime I started the server. This setup was really helpful in early
stage development, where the database changes a lot(without requiring
me to update the database schema), and I'm trying to achieve similar
results with django/sqlite in memory databases.
I placed some startup code in a middleware as follows :

from django.core.exceptions import MiddlewareNotUsed
from django.conf import settings
from django.core.management import call_command
from books.models import *
from django.contrib.auth.models import User

class StartupMiddleware(object):

def create_publishers(self):
p1 = Publisher(name='Apress', address='2855 Telegraph Avenue',
city='Berkeley', state_province='CA', country='U.S.A.',
website='http://www.apress.com/')
p1.save()
p2 = Publisher(name="O'Reilly", address='10 Fawcett St.',
city='Cambridge', state_province='MA', country='U.S.A.',
website='http://www.oreilly.com/')
p2.save()

def create_users(self):
u1 = User(username='thiago', email='thi...@pass.com')
u1.set_password('somepass')
u1.save()

def create_stubs(self):
self.create_publishers()
self.create_users()

def __init__(self):
if settings.DATABASE_NAME == ':memory:':
call_command('syncdb', interactive=False)
self.create_stubs()
raise MiddlewareNotUsed('Startup complete')


This is not working(At least I can't logon to the admin site with
that user). I'm unsure if middleware is the best place to place
startup code, or If I'm going the right way. Any help is appreciated.

Thanks.

Shawn Milochik

unread,
Aug 17, 2010, 11:05:14 AM8/17/10
to django...@googlegroups.com
Middleware executes with each request, so it's probably not the best
place for such a thing.

It seems like it would be a lot easier to use a normal sqlite3
database and just delete it after each run.

Also, if you have test data you'd like to reload regularly, I
recommend using a regular database, creating your test data in the
admin, then using ./manage.py dumpdata to create a fixture. Then, you
could use that fixture for your development and your unit tests.

Shawn

Thiago Padilha

unread,
Aug 17, 2010, 11:21:12 AM8/17/10
to Django users
Ok I managed to get that working, when I start the server I get :

Django version 1.1.1, using settings 'mysite.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Creating table django_admin_log
Creating table auth_permission
Creating table auth_group
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table books_publisher
Creating table books_author
Creating table books_book

the only problem is that when I make the first request it blocks for
console input with the following message :

You just installed Django's auth system, which means you don't have
any superusers defined.
Would you like to create one now? (yes/no)

If I create a user with the same name created in the startup
middleware I get unique constraint database violation, if I choose no
everything proceeds smoothly and I can log on with my stub user. I
don't want the first request blocking everytime to ask for a new user,
can I remove that prompt?

Thanks.

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>

Shawn Milochik

unread,
Aug 17, 2010, 1:09:04 PM8/17/10
to django...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages