0

After connecting to the Django ORM as described here and here.

What's the best practice to avoid a runtime error if there already is an active connection/setup/settings already configured?

Currently if you call settings.configure(...) again you get an exception: RuntimeError: Settings already configured. I want to avoid potentially making multiple connections and this runtime error when already setup.

Is there a way to disconnect gracefully or should this be try/catched?

pyCthon
  • 11,746
  • 20
  • 73
  • 135
  • This has nothing to do with "connections". – Daniel Roseman Jul 09 '17 at 18:15
  • Well the setup makes a connection to the db doesn't it? – pyCthon Jul 09 '17 at 18:32
  • 2
    No, configuring settings does not create a connection. The database backend lazily creates a thread-local connection when a query is executed, unless a connection is already active. – knbk Jul 09 '17 at 19:17
  • so `try settings.configure(...) except RuntimeError, e: print e` is the way to go? – pyCthon Jul 09 '17 at 19:29
  • 1
    You should just call `settings.configure()` once at the entry point of your application, or alternatively set `os.environ['DJANGO_SETTINGS_MODULE']` to point to your settings module if you have one. – knbk Jul 09 '17 at 19:37

0 Answers0