AppConfig where to execute initialization queries?

71 views
Skip to first unread message

Marc Aymerich

unread,
Oct 6, 2014, 8:56:15 AM10/6/14
to django-users
Hi,
I have been bitten by unexpected behaviors when running tests because on AppConfig.ready() I was initializing some stuff that depends on DB stored data. Reading the django docs I've found a warning that specifically suggests not to do this [1]. But then I wonder where this kind of initialization logic should go? any suggestion?

Although you can access model classes as described above, avoid interacting with the database in your ready()implementation. This includes model methods that execute queries (save()delete(), manager methods etc.), and also raw SQL queries via django.db.connection. Your ready() method will run during startup of every management command. For example, even though the test database configuration is separate from the production settings, manage.py test would still execute some queries against your production database!


--
Marc

Collin Anderson

unread,
Oct 7, 2014, 12:49:51 PM10/7/14
to django...@googlegroups.com
Could you somehow lazy initialize? Somehow auto-initialize things the first time they are called for?

The other, kind of hacky place to do it is at the top of your urls.py, which should run on the first request.

Oliver George

unread,
Oct 22, 2014, 3:50:41 PM10/22/14
to django...@googlegroups.com
Did you find a good solution for this?
Reply all
Reply to author
Forward
0 new messages