I am using mysql database. I have many schemas with many tables. I want to create a Django admin interface for various tables in different schemas. Currently for a single schema I am using a setting like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'schema1',
'USER': 'test',
'PASSWORD': 'test',
'HOST': 'example.abc.com',
'PORT': '',
}
After creating an app, admin interface is created for whatever models I register in the admin.py of that app for this schema. Now I want to create an other app where I register models of another schema in its admin.py. These models will belong to different schema. Now how do I create an admin interface for the second app that points to different schema?
Is it possible to create two Django projects with two different settings.py and two different admin interfaces? (So that each will point to different schema.)
I have googled a lot about this. But couldn't find any info. May be there is a simple way and I am approaching this in a wrong way. Any help is appreciated.
Thanks in advance.
This is documented well on the django doc here http://docs.djangoproject.com/en/dev/topics/db/multi-db/#exposing-multiple-databases-in-django-s-admin-interface
I'm not quite sure if you mean that you want to handle different databases or just have different models registered. If you want to have different models in different admin sites, you can register multiple admin sites with different models. You can then access one site eg with '/admin' the other with '/otheradmin'. Maybe you find django-admin-tools useful to customize the display of your models/apps within the admin!
Related
Problem:
I am having a website which has various apps (Accounts, Community, Company, Payments, Main_project_app).
What I want is to set up a google type similar architecture where the userbase is on one server and other apps such as Gmail, drive, etc. serve the specific services on different servers based on the user authentication from the main Google userbase server i.e. Accounts app which have information about the user such as username, email, password, etc must remain on one server and using the REST API's or something we connect accounts app with another server to take the user from sign-in page to dashboard and serve them other services by using all other apps.
I am a newbie so please if I have done some mistakes to explain or something do correct me.
TLDR: django can not do cross database foreign key lookups, which is hard to solve as a beginner, therefore django may not be the right tool, or the approach of splitting the database has to be omitted.
As you probably can imagine your question goes very far in terms of creating a distributed system and as these systems have a million options of how you want them to function and how they are interconnected with a database cluster.
But I can try to give you an overview and the first problem you will be likely confronted with.
where the userbase is on one server and other apps such as Gmail, drive, etc. serve the specific services on different servers
Okay, from what I understand you want to split your database by model type. Specifically you want the User model to be on a different database server with a different django instance doing authentication. This can basically be done by splitting the database locally.
Let us call the server with the User database the AuthServer and another server the AppServer. The AuthServer must serve an API that receives user credentials and decides whether the user can log in or not. Meanwhile the AppServer needs to cache User instances locally so it must not reauthenticate the user on every request. That is easily done by implementing a custom AuthenticationBackend. Now the AppServer receives a login request, can pass it to the AuthServer and receive a user instance on success or not on login failure.
This works well even with relationships between User and other app models because AuthServer and AppServer can use the same database server. But this somehow beats the purpose of splitting them up as you want to do it in the first place. Therefore django offers the approch of having multiple databases (the documentation here even uses that exact example that I will copy the code from). In your settings file you can store multiple database connections.
DATABASES = {
'default': {
'NAME': 'app_data',
'ENGINE': 'django.db.backends.postgresql',
'USER': 'postgres_user',
'PASSWORD': 's3krit'
},
'users': {
'NAME': 'user_data',
'ENGINE': 'django.db.backends.mysql',
'USER': 'mysql_user',
'PASSWORD': 'priv4te'
}
}
Further you can now write a DatabaseRouter that will probably look something like this (incomplete example):
class AuthRouter:
route_app_labels = {'auth', 'contenttypes'}
def db_for_read(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return 'users'
return 'default'
def db_for_write(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return 'users'
return 'default'
For completeness you have to add the router to the settings
DATABASE_ROUTERS = ['path.to.AuthRouter', ]
Now the first problem you are going to have is if you want to connect the User model to any other model, which is extremely likely. Django does not allow to have cross database relationships in terms of foreign keys to models in other databases. You can circumvent the issue in a very complicated manner by using IntegerField instead of ForeignKey fields and care for the datasbase integrety for your self but I will no recommend you to do it.
After all django is not the right tool to do such widespread services if you are on a beginner level as you say, or you have to prepare for very complicated and bug and error prone xperience :)
I have two databases.
default has several models and works fine by itself.
When I add another database, it tries to creates tables from default database in the new database also. the new database does not have any models. All I am using it is to make a direct SQL query.
DATABASES = {
'default': {
'ENGINE':
..
..
},
'payments': {
'ENGINE':
..
..
},
}
Django does allow for multiple databases. You have to explicitly route to them for specific models and such though. In your case, it sounds like you will want to route all of your django models to the default DB and define specific cases for when to query the payments DB. I have linked a page, and specifically one section of that page for you to reference. I could copy and paste pieces of that page, but I think you'll find it more useful to just follow that page's explanation.
You should find this page helpful: MultiDB
This is particular will answer your question on managing two databases together: managers
I am considering using Google BigQuery as a back-end for Django but cannot be certain if this is possible, and if it is, what settings would apply.
Currently, my Django application uses Postgresql, and the code in settings.py is as follows:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
Ideally, I'd like to setup a database connection to Google BigQuery through settings.py and then use views and models as usual.
It's not possible, or at least not supported. You could use the API directly, but obviously you won't get any advantages of the ORM.
You should have a db motor like postgres, mysql, whatever...
The point is, this db motor is necessary to have it, because the structure works in that way.
but of course, you can invoke google cloud from librarys in django and use it as
from google.oauth2 import service_account
from google.auth.transport.requests import AuthorizedSession
from google.cloud import datastore
from google.cloud import bigquery
in my case I used to connect
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'OAuth2Credential.json'
for generate your .json you should go to the documentation in:
https://cloud.google.com/iam/docs/creating-managing-service-account-keys
It is possible by using SQLAlchemy with Django.
SQLAlchemy can connect to bigquery with the pybigquery driver.
See the following about how to
Configuring Django to use SQLAlchemy.
Everything is possible for sure. To create an interface would not be such a big job. But, I would just keep one note:
Bigquery is not intended to be backend database, rather it is more like data warehouse as it is defined within business intelligence discipline. This means, google will make it very hard for you to perform fast multi-user operations. As far as I can recall, update statements for example have have some thresholds.
On another hand, if this is purely for example data input, or visualisation of data, then why not. But then again, I think Azure power apps is kind of a product for it.
I am new to Django; intermediate in Python3.6. Now, I've created a basic Django app which displays index.html. Now, I want to create a very simple form in index.html which will query the database and print the results in the same form. I already have the postgresql database in my local with all the data needed; it is very simple with just one table (right now, my Django app is connected to the default sqlit3 db). Where do I go about creating the query to fetch the data and populate it?
You need to install these libraries first :
sudo apt-get install libpq-dev postgresql postgresql-contrib
Configuration
in settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'db_name',
'USER': 'db_user',
'PASSWORD': 'db_user_password',
'HOST': '',
'PORT': 'db_port_number',
}
}
You can refer to these two links:
For More Detailed information about setup :Tutorial for setting up application using PostgreSql
Documentation link for writing Queries
Glad you're coming on board with Django! It's a great language with plenty of useful features, you won't regret learning it.
To connect to an existing database, check out the docs here. This explains how to use your existing db to create models in your project.
I also recommend checking out the tutorials in the docs starting here. This tutorial will explain how to connect to your postgres db, as well as how to begin writing queries for your views. This is the place to start learning the basics of Django, just follow it along and you will get the hang of everything in no time
Can django use an external database? I mean, if you have one server for the db and other n-servers with the web server, can django use the db on an external machine? Can django do queries via internet to another django db?
Yes, your database and web server can be on separate servers. You just have to specify in your settings file the host. See https://docs.djangoproject.com/en/dev/ref/settings/#databases
As the relevant documentation states, Django is capable of using multiple databases. Whether remote access is supported will depend on which one you choose to use - but, as a general rule, it is supported, with the notable exception of sqlite
Yes you can access the database from anywhere. But they(you) need to provide database privileges for your IP. some code is like,
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': Remote Host,
'PORT': '5432',
}
}
You can also use external django package https://github.com/kennethreitz/dj-database-url for deployment.
I reach this post, but in my case, I need to access another database. My intention is create an application that report informationa from another database. I needed to use two databases. I found the way in the django documentation.
https://docs.djangoproject.com/en/3.2/topics/db/multi-db/