How to two apps from django to conect one db postgresql - django

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.)

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).

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 communicate with an external server using django application?

Currently I have a requirement,I need to communicate with external server using django application. The server is already up,Next section is the data transfer. I need some time samples and values from server and I need to send responses back to server. How django port can listen to external server.? How it can response back? I need asynchronous communication and REST responses
According to the comments, the external server is a PostgreSQL database.
Since you're using Django, you can easily set this up as a secondary database in your settings:
DATABASES = {
'default': # that SQLite config...,
'game': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': 'somewhere-else',
'PORT': '5432',
},
}
You don't have to route any models to that database, but if you do create any models to correspond to the data in the game server, you'll want to set managed = False so Django doesn't do migrations or anything.
If you don't want to use models, just open a cursor to the secondary database and query away:
from django.db import connections
with connections['game'].cursor() as cursor:
cursor.execute('SELECT something FROM some_table')
# etc...

Way to query CloudSql from Django app

I am trying to find an effective way to query a Google Cloud SQL database from my Django app. This app is not hosting on App Engine, just on a local server.
I have found a couple of links:
This one is a generic python connector
This answer mentions a way to do it from a local Django app, but it looks like it is more for testing.
I am not looking to use CloudSQL as my app backend, just make occasional queries to it (probably something daily to read all records in one CloudSQL table, and update a local database with the result)
This is possible, and not just for testing. You want something like:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'name_of_your_database',
'USER': 'root',
'PASSWORD': 'your_cloud_sql_root_password',
'HOST': 'assigned_IP_of_your_cloud_sql_instance',
'PORT': '3306',
}
}
Where name_of_your_database was the database you created with CREATE DATABASE.
You may also want to use SSL to protect your data as it goes over the public internet. To do this configure SSL for your instance and then add the following to the default django database options:
'OPTIONS': {
'ssl': {
'ca': '<PATH TO CA CERT>',
'cert': '<PATH TO CLIENT CERT>',
'key': '<PATH TO CLIENT KEY>'
}
}

How to select another DB in Django's ORM when used in a desktop application?

I'm writing a desktop application with PyQt where we planned to use sqlite3-databases for file storage (instead of pickles, XML, YAML, etc). The reason is that our application is likely to migrate to a centralized data store later. (which then needs to communicate with other web based services, etc etc.)
Before everyone says "use SQLAlchemy and Elixir", I'd like to point out why chose Django, namely because:
I know Django pretty well, it's neat and I like it's ORM.
When we migrate, it's easy to add a web-ui on top of it.
Having the Admin-interface makes it easy to debug/inspect the DB during development.
Anyway, my problem is that I can't select different sqlite3 databases, since Django's settings.configure throws an 'already configured' error on the second call.
Any ideas, apart from restarting the app?
(None of many Django-desktop-orm questions here on SO seem to address this...)
Paraphrasing http://docs.djangoproject.com/en/dev/topics/db/multi-db/
Define multiple DBs in the settings.py.
DATABASES = {
'default': {
'NAME': 'defaultdb',
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'USER': 'postgres_user',
'PASSWORD': 's3krit'
},
'other': {
'NAME': 'otherdb',
'ENGINE': 'django.db.backends.mysql',
'USER': 'mysql_user',
'PASSWORD': 'priv4te'
}
}
Then you can choose the database manually.
>>> # This will run on the 'default' database.
>>> Author.objects.all()
>>> # So will this.
>>> Author.objects.using('default').all()
>>> # This will run on the 'other' database.
>>> Author.objects.using('other').all()