Search code examples
djangomultiple-databases

In Django 1.6, how to find what database a model object is from?


In Django, in a project using multiple databases and given a model object, how can I find out the name of the database (corresponding to the names used in using and configured in settings.DATABASES) the object is from?


Solution

  • You can do: <object>._state.db to get the database the object is stored in.

    Here is the relevant source

    Also here is the documentation (no very obvious though.. )