Using Django (and DRF)
If I'm set ATOMIC_REQUESTS=True on the Database config, would this apply for signals as well?
I'm trying to understand if there is a chance our transaction is finished after response
Related
I am working on a Python/Django project. I use Django signals in order to do an operation after creation of new instances of a model.
The question is: Is there any trick to access the request that has triggered this signal? e.g. to use its user field and do some operations based on it.
I'm using django with django-tenants for a SaaS app and it is using the user's subdomain to set the db schema in postgresql. So a call to user1.app.com will use the 'user1' schema.
Each call to a view creates a new database connection by default. If the view call ajax, a new connect/disconnect happens. Using this method works as each call gets it's postgresql search path set to the correct schema using the django-tenants middleware.
My question is if I set CONN_MAX_AGE to persist connections for a period of time to reduce the heavy connect/disconnect load on the server, will django pool connections from multiple users potentially using different schemas? I am concerned, because:
All users are using the 'default' DATABASE. (just the search path changes)
I don't see any logic that binds a connection to a unique user session.
The built in development server ignores the CONN_MAX_AGE setting so I can't test locally.
I have looked into the connection_created signal, but I would be duplicating logic with django-tenants and I'm not sure of any unintended consequences.
Using django 4.0.7 and posgresql 12.
I have read lots of documentation and articles about using signals in Django, but I cannot understand the concept.
What is the purpose of using signals in Django?
How does it work?
Please explain the concept of signals and how to use it in Django code.
The Django Signals is a strategy to allow decoupled applications to get notified when certain events occur. Let’s say you want to invalidate a cached page everytime a given model instance is updated, but there are several places in your code base that this model can be updated. You can do that using signals, hooking some pieces of code to be executed everytime this specific model’s save method is trigged.
Another common use case is when you have extended the Custom Django User by using the Profile strategy through a one-to-one relationship. What we usually do is use a “signal dispatcher” to listen for the User’s post_save event to also update the Profile instance as well.
I am creating an app where I store the USERS in a Postgres database with the help of the standard User Model, in my Django app i use Django queries to get all needed information, like "first_name", "username" .. etc
I implemented Django_auth_ldap to start storing user identification data in an Openldap server if i want. But now, i'm confused to how to get the data i used to get using django queries. i don't want to change the behavior in my views, i want to continue using Django queries
This looks like it describes some of what you want: https://django-auth-ldap.readthedocs.io/en/latest/users.html
You can perform arbitrary population of your user models by adding listeners to the Django signal: django_auth_ldap.backend.populate_user. This signal is sent after the user object has been constructed (but not necessarily saved) and any configured attribute mapping has been applied (see below). You can use this to propagate information from the LDAP directory to the user object any way you like. If you need the user object to exist in the database at this point, you can save it in your signal handler or override get_or_build_user(). In either case, the user instance will be saved automatically after the signal handlers are run.
How does Django rest framework handles request ? Is it process based or it thread model.
For each request does one process start a new thread or its just one process which takes care of all request (like it happens in NodeJS) based on context.
Your question is about Django and not DRF. That said, I believe Django is single-threaded, but thread-safe. You might want to have a look to Celery if you are interested in deferred tasks.