Executing DB-Queries during import

61 views
Skip to first unread message

guettli

unread,
Mar 22, 2017, 12:16:16 PM3/22/17
to Django users
If I recall correctly, then you should not execute db-queries during import.

I think this can do a lot of trouble. For example during test-discovery db-queries get executed.

Is it just me being paranoid, or is this a general advice?

I tried to find something about this topic with my favorite search engine, but failed.

What do you think?

Regards,
  Thomas Güttler

Tim Graham

unread,
Mar 22, 2017, 2:20:43 PM3/22/17
to Django users
Correct, module level queries should be avoided. Django might try to add some detection to warn about or prohibit that, see https://groups.google.com/d/topic/django-developers/7JwWatLfP44/discussion.

guettli

unread,
Mar 23, 2017, 5:51:20 AM3/23/17
to Django users
Thank you tim for the link.

Since on the referenced discussion someone asked for help to debug this, I published my solution here: http://stackoverflow.com/questions/42972444/django-detect-module-level-db-queries/42972445#42972445

I think it would help if you get a RuntimeException if you access the db too early

Regards,
  Thomas

guettli

unread,
Mar 29, 2017, 8:39:52 AM3/29/17
to Django users


Am Mittwoch, 22. März 2017 19:20:43 UTC+1 schrieb Tim Graham:
Correct, module level queries should be avoided. Django might try to add some detection to warn about or prohibit that, see https://groups.google.com/d/topic/django-developers/7JwWatLfP44/discussion.


I understand db queries at module-level (aka "import time") should not happen.

When I am allowed to do db queries.

Is it advisable to execute db queries in my AppConfig ready signal handler?

Use case: I want to add URL patterns which get collected from db rows.


Simon Charette

unread,
Mar 29, 2017, 6:55:40 PM3/29/17
to Django users
> Is it advisable to execute db queries in my AppConfig ready signal handler?

AppConfig.ready() is still at module-level.

> Use case: I want to add URL patterns which get collected from db rows.

It might be more appropriate to define an URL resolver (an object like django.conf.url)
that performs queries at resolve()/reverse() time instead.

A simpler solution could be to define a catch all view like django-cms does and
perform the routing inside the view.

Cheers,
Simon

guettli

unread,
Apr 4, 2017, 6:57:54 AM4/4/17
to Django users


Am Donnerstag, 30. März 2017 00:55:40 UTC+2 schrieb Simon Charette:
> Is it advisable to execute db queries in my AppConfig ready signal handler?

AppConfig.ready() is still at module-level.


ok. Good to know.

 
> Use case: I want to add URL patterns which get collected from db rows.

It might be more appropriate to define an URL resolver (an object like django.conf.url)
that performs queries at resolve()/reverse() time instead.

A simpler solution could be to define a catch all view like django-cms does and
perform the routing inside the view.


Yes, you are right. This looks like a good solution.

Thank you very much for sharing your knowledge.

Regards,
  Thomas Güttler
Reply all
Reply to author
Forward
0 new messages