Change schema on views using Django Tenants - django

I'm using Django Tenants on my project and I'm creating a schema for each Tenant.
I have 'django.contrib.auth' and 'django.contrib.contenttypes' both in SHARED_APPS and in TENANT_APPS, and now I want to create specific groups in each tenant schema.
The problem is that I'm always reading and writing values from the public schema.
I implemented the following:
DATABASES = {
'default': {
'ENGINE': 'django_tenants.postgresql_backend',
'NAME': 'DB_NAME',
'USER': 'DB_USER',
'PASSWORD': 'DB_PASS',
'HOST': 'DB_HOST',
'PORT': 'DB_PORT',
}
}
DATABASE_ROUTERS = (
'django_tenants.routers.TenantSyncRouter',
)
How can I change to a different schema? Can I do it on an app views?

Found a solution,
from django_tenants.utils import schema_context
my_schema_name = 'volvo'
from django_tenants.utils import schema_context
with schema_context(my_schema_name):
#do what you want here
Basically, it was the same problem as here, but with a different library (django-tenants). After checking their source code, found out that they had the same method.

Related

How to share database details in Github correctly, using Django and PostgreSQL?

I have an assignment in which I need to set up a database server using Python Django and PostgreSQL. I need to assign the project in Github, and the grader will use my repository to check my project.
In my setting.py file I have the following lines:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'CourseDB',
'USER': 'postgres',
'PASSWORD': '123',
'HOST': 'localhost',
'PORT': '5432'
}
}
What to do so the details on my file will be correct for the grader's side?
Will they have to create a database with the given name, user and password like the ones in my file?
I think that maybe for the database name, I can add in the readme to run CREATE DATABASE CourseDB first. But then again, I don't know their user and password on their machine, So I don't know what should be written in my file in order for my code to work on their machine.
I followed this tutorial on YouTube to create my file.
Unless you need some Postgres specific functionality, you can use the default SQLite backend:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
Django will automatically create a database file. No setup/user/password needed.
In general, a good practice is to save all the DB sensitive credentials in settings.py using env vars like the following:
import os
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ['DATABASE_NAME'],
'USER': os.environ['DATABASE_USER'],
'PASSWORD': os.environ['DATABASE_PASSWORD'],
'HOST': os.environ['DATABASE_HOST'],
'PORT': os.environ['DATABASE_PORT'],
}
}
Several DB backends are natively supported (see the official docs here) including PostgreSQL, MariaDB, MySQL, Oracle and SQLite and a number of additional backends provided by third parties.
If you go with the SQLite backend (for sure the best alternative for quick development and MVPs), keep in mind that there are some differences specific to the SQLite backend that you should take into consideration (e.g. not supported features).

Connecting a Neo4j graph database to a Django REST API app

I am trying to connect a remote Neo4j database to a Django app with a REST API.
I am trying to specify the database in the settings.py file using the following code:
DATABASES = {
'default': {
'NAME': 'papers.db',
'ENGINE': 'django.db.backends.sqlite3',
'USER': '',
'PASSWORD': '',
'PORT': '',
},
}
I would like to know:
What python libraries need to be installed in order to do this?
What is the 'ENGINE' that needs to be specified in the code above?
The Neo4j database has a URI, but does not provide us with an IP address - am I able to use this URI?
I am confident that I know what the other parameters need to be.
Thanks in advance

Extracting data with all fields from a postgresql database and creating a new model with some new fields in django

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': '*****',
'USER': '******',
'PASSWORD': '*****',
'HOST': '******',
'PORT': '***',
}
}
I am using a postgresql sql database hosted online .The thing is I want to create a new model with all common fields and some additional fields.Can anyone help me with how should i proceed?.Also im using django-mailbox to import the messages and attachments.
I believe what you are searching for is python manage.py inspectdb, coming from the django documentation about integrating with legacy databases.
It will inspect an existing database and create unmanaged models for you that you can use similar to normal models in your application.

How to two apps from django to conect one db postgresql

I have a little question.
I have an app in Django rest framework with PostgreSQL(the project called djangoFall), and I build other projects with Django called djangoRuim, but I don't know how to connect and read the tables in the djangoRuim for example
djangoFall connect with the PostgreSQL is working
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'colonybitdb0',
'USER': 'postgres',
'PASSWORD': 'root2017',
'HOST': '127.0.0.1',
'PORT': '5432',
} }
in here I can read tables like this
from djangoFall.profile_clbt.models import HelperNotificationMsg
djangoRuim connect with the same connect PostgreSQL DB is working but I don't how to read the tables
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'colonybitdb0',
'USER': 'postgres',
'PASSWORD': 'root2017',
'HOST': '127.0.0.1',
'PORT': '5432',
}}
in here I can't read the same table
from .models import HelperNotificationMsg # wrong true ?
because here I don't have models.
please help me, how to read these tables.
You can use the connection object to execute direct SQL query to the table's name generated by Django in the second app or the value you assigned to db_table in the Meta option in the second app models.py. This will work here because you are using the same database settings for the two apps.
In case of different database settings, you will need to explicitly connect to the external database with the driver and execute the SQL query.
That being said, since you are using the same database, I think you should consider merging those two apps, or package one. But it depends on what you're trying to archive and the overall architecture.
If it is really mandatory to access to the very same database from two different django applications, here is what needs to be done:
Create the same object model in both applications, make sure each object member are identical.
Use the same credentials and url to access the database.
Now, both of these applications can access to the database with implicit race condition protection, presuming that the database has native support for that. (All modern databases have that support.)

How to divide a large Django project into sub projects for scaling?

Django project(e-commerce website) that we need to divide into sub-projects as a Buyer, Seller, and Admin and will create three databases accordingly. So how can we manage the same Models(Schema) in 3 projects if it is updated in one project?.
Django support multiple database features for same project.
This can be achieved by defined multiple database property in settings.py as
DATABASES = {
'default': {},
'buyer': {
'NAME': 'buyer',
'ENGINE': 'django.db.backends.mysql',
'USER': '',
'PASSWORD': ''
},
'customers': {
'NAME': 'customers',
'ENGINE': 'django.db.backends.mysql',
'USER': '',
'PASSWORD': ''
}
}
Need to define router along with this.
https://docs.djangoproject.com/en/3.0/topics/db/multi-db/#using-routers
Also manually can be selected database by using keyword
eg.
## fetch
Customer.objects.using('customers').all()
## save
customer_obj.save(using='customers')
Ofcourse there is some limitation with multiple database such as
Cross-database relations
Django doesn’t currently provide any support for foreign key or many-to-many
relationships spanning multiple databases.
Behavior of contrib apps
Several contrib apps include models, and some apps depend on others. Since cross-database relationships are impossible, this creates some restrictions on how you can split these models across databases:
https://docs.djangoproject.com/en/3.0/topics/db/multi-db/#behavior-of-contrib-apps
The official link can be followed for more details:
https://docs.djangoproject.com/en/3.0/topics/db/multi-db/