how does "get_new_connection" work in django ?

Hi guys, I am learning the django.db code.

I have a question about the “get_new_connection” function in django/db/backends/base/base.py
I search the entire django project.


I can see the function definition of “get_new_connection”. but I cannot see where it is called.
anyone can tell where this function is called ?
$ git grep -i get_new_connection
django/contrib/gis/db/backends/spatialite/base.py:    def get_new_connection(self, conn_params):
django/contrib/gis/db/backends/spatialite/base.py:        conn = super().get_new_connection(conn_params)
django/db/backends/base/base.py:        """Return a dict of parameters suitable for get_new_connection."""
django/db/backends/base/base.py:    def get_new_connection(self, conn_params):
django/db/backends/base/base.py:        raise NotImplementedError('subclasses of BaseDatabaseWrapper may require a get_new_connection() method')
django/db/backends/base/base.py:        self.connection = self.get_new_connection(conn_params)
django/db/backends/mysql/base.py:    def get_new_connection(self, conn_params):
django/db/backends/oracle/base.py:    def get_new_connection(self, conn_params):
django/db/backends/postgresql/base.py:    def get_new_connection(self, conn_params):
django/db/backends/sqlite3/base.py:    def get_new_connection(self, conn_params):

It’s called here: https://github.com/django/django/blob/3259983f569151232d8e3b0c3d0de3a858c2b265/django/db/backends/base/base.py#L200

If you’re relying on GitHub code search to find references, it only shows one instance per file. It’s more reliable to use local grep or editor search.

see it . thanks.

I am a bit confusing about how a model method (like save()) get a db connection.
when django.db started, it will init the settings.DATABASES into a connections dict.
if I am using postgresql backend. and I call a model.save()
if I don’t specify the “using” parameter it will use “defalut” db. but how does the save() method reach to default db using the connections dict ?

I’m afraid I don’t have time to walk you through the code. This talk may be of interest for exploring the ORM: https://www.youtube.com/watch?v=tkwZ1jG3XgA

You don’t need to know the full path to use the ORM!

you help me a lots !! thanks!!!