Django - How can I INSERT values to a second database - django

I've a Django project that I need to start storing the hits on pages. I'm thinking to do it to a second database. The models on this second database will be not present in the Master database.
Question 1:
Django will handle this kind of thing? It is possible to create the models only to a second database?
Question 2:
Is this the best approach, to write this kind of data to a second database and use a queue system like RabbitMQ to handle these INSERTS.
Question 3:
There is out there any examples on how can I setup a second Database on Django and write to it?
Best Regards,

Yes it will. The database setting allows more than one database (it's a dictionary).
You will have to customize a lot of things though.
See https://docs.djangoproject.com/en/dev/topics/db/multi-db/ for further specs
DATABASES = {
'default': {
'NAME': 'app_data',
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'USER': 'postgres_user',
'PASSWORD': 's3krit'
},
'users': {
'NAME': 'user_data',
'ENGINE': 'django.db.backends.mysql',
'USER': 'mysql_user',
'PASSWORD': 'priv4te'
}
}

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

django unit testing on multiple databases

I'm working on a django project where all my unit test cases were working perfectly.
Ass soon as I introduced a second database all my test cases that inherit from TestCase are broken. At this stage I haven't build any test case for that second database but my router is working fine.
When I run the tests I get the error,
"KeyError: 'SUPPORTS_TRANSACTIONS'"
It appears to me that is trying to check that that all the databases that I've got setup support transactions but the second database is never created.
Any ideas on how to have the test script to build the second database.
I realise this is quite an old thread, but I ran into it with the same issue, and my resolve was adding the multi_db = True flag to my testcase, e.g:
class TestThingWithMultipleDatabases(TestCase):
multi_db = True
def test_thing(self):
pass
Source https://github.com/django/django/blob/master/django/test/testcases.py#L861
This causes django to call flush on all databases (or rollback if they support transactions)
I too am using a db router
I'm afraid I cant find this in Django's documentation, so no link for that
yes I had a similar problem... my fix was to set 'SUPPORTS_TRANSACTIONS': True for each of the database connections in the settings file. Not sure if this is the correct way to fix it, but it worked for me.
'SUPPORTS_TRANSACTIONS':True worked for me too.
However I have a kind of weird multiple db setup using database routers.
#user298404: how does your multiple db setup look like?
ps. sorry; not enough points for comment...
Here is a multiple db setup that I currently have in production:
DATABASES = {
# 'default' is used as the WRITE (master) connection
DB_PRIMARY_MASTER: {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'main',
'USER': 'main_write',
'PASSWORD': 'XXXX',
'HOST': 'db-master',
'PORT': '3306',
'SUPPORTS_TRANSACTIONS': True,
},
# Slave connections are READONLY
DB_PRIMARY_SLAVE: {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'main',
'USER': 'main_read',
'PASSWORD': 'XXXX',
'HOST': 'db-slave',
'PORT': '3306',
'TEST_MIRROR': DB_PRIMARY_MASTER,
'SUPPORTS_TRANSACTIONS': True,
},
# 'mail_default' is used as the WRITE (master) connection for the mail database
DB_MAIL_MASTER: {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dbmail',
'USER': 'dbmail_write',
'PASSWORD': 'XXXX',
'HOST': 'db-mail-master',
'PORT': '3306',
'SUPPORTS_TRANSACTIONS': True,
},
# Slave connections are READONLY
DB_MAIL_SLAVE: {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dbmail',
'USER': 'dbmail_read',
'PASSWORD': 'XXXX',
'HOST': 'db-mail-slave',
'PORT': '3306',
'TEST_MIRROR': DB_MAIL_MASTER,
'SUPPORTS_TRANSACTIONS': True,
},
}
DB_PRIMARY_MASTER, DB_PRIMARY_SLAVE, DB_MAIL_MASTER, and DB_MAIL_SLAVE are all string constants so that they can be used in my database router.
Hint: DB_PRIMARY_MASTER='default'
I hope this helps!
Referring to that link
Django doc Multi-Db
you can:
from django.test import TransactionTestCase
class TestMyViews(TransactionTestCase):
databases = {'default', 'other'} # {'__all__'} shold work too
def test_index_page_view(self):
call_some_test_code()
thanks to
#sih4sing5hog5