2

If you use Django you can simply create an instance of one of your models, fill it with data and call save() on it and it will be saved to the database. You don't have to pass in a "connection" parameter or do anything special. Also, your view are just simple callables so there seems to be no magic hidden. I.e. this works:

from django.http import HttpResponse
from models import MyModel

def a_simple_view(request):

    instance = MyModel(some_field="Foobar")
    instance.save()

    return HttpResponse(<html><body>Jep, just saved</body></html>)

So the question is: How does my freshly created model instance get a database connection so save itself? And as a followup: Is this is a sensible way to do it?

1

2 Answers 2

6

How does my freshly created model instance get a database connection so save itself?

Essentially, each model has a Manager that knows the database connection. In reality it is a bit more complicated, because the manager delegates the connection creation and management (to database routers and connection managers).

Is this is a sensible way to do it?

Well, that's a question that cannot be answered without context, really. In the context of what a Django model is, this is the sensible approach because as a developer you do not have to concern yourself with connection management.

If you're asking whether Django takes a sensible approach to connection management, and you are worried it may not, here's what the Django documentation has to say about it:

Django opens a connection to the database when it first makes a database query. It keeps this connection open and reuses it in subsequent requests. Django closes the connection once it exceeds the maximum age defined by CONN_MAX_AGE or when it isn’t usable any longer.

and:

Since each thread maintains its own connection, your database must support at least as many simultaneous connections as you have worker threads.

So now the question is: when and how many threads are created? This depends on the server used. E.g. the development server starts a new thread for every request, whereas gunicorn reuses threads across requests.

Sign up to request clarification or add additional context in comments.

Comments

0

It is hard to describe how django does this but you can probably learn more about this by looking at the source code of django and specifically on django.db module.

There's a DB abstraction layer so that Django works with many databases such as sqlite, mysql and postgresql. There's connection pooling so that django reuses the connection on subsequent queries. All these things are used by a django model when a db query is to be run. In the end it is not simple and you should check the source code to find detailed answers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.