Django multiple database support - django

Using Django 1.4 is it possible to have 2 apps within the same project use two different databases?
If so, will each app have there own settings.py?

Have a look at the django documentation on how to define multiple databases in your settings.
To use a different database for each app you can use one of to approaches:
You can manually select a database on each database call. This will probably not be possible if you used third party apps.
You can define a database router which is probably more suitable if you do not want to modify an app. There is an example in the documentation which explains how to route reads/writes to another database.

Related

How to run multiple sites from one instance of Django?

I am using Django as the back end and Nuxt as the front end, with Postgres as the database. I would like to have different tables/databases for each domain.
The Django code and the Nuxt code will be the same for all sites.
The way the app is set up, it requires different domains to have different databases or at least different tables.
At the moment, I am thinking I need to create a separate installation of Django/Nuxt for each domain. But I am wondering if there is a better way?
Setting up different Code bases for every domain is not a good idea at all. I had an app that was set up that way and soon we were struggling to maintain the same code and DB structure for all the different domains. After a few domains, it will become tricky.
I would suggest developing a multi-tenant SAAS based app, where you can have multiple tenants running the same Django/Nuxt code but having different schemas in the Postgres, So your database for different domains will be completely separate from each other. I would suggest using django tenant schemas for this. Even if it takes you some time to set it up, I would suggest doing it. It will save a great deal of time for you in the future.

Two flask apps (models) using same table

I build 2 flask Apps App1 and App2(Two different services). Both Apps are referring to the same DB. Am using MongoDB as database and MongoEngine to create connections and to support ORM Queries.
I have created a user table in App1 and I defined the structure of the table in models.py file. Now I have to use the same user table in the App2. How would I use the existing table itself without rewriting the same code in APP2?
I can do it in one way that I can write a Mongo wrapper which will connect and serve the data. But I don't want to write RAW queries. Can someone help me how to do this? Thanks!
You can put all your database-related code into a separate Python package which both your applications can then import.
OR
You could also consider building a separate application around your database code that exposes information through an API. Your other applications could then make requests to this API.

django - how can I write data to an external Mongo database?

I have a Django app that is using PostgreSQL. Everything is fine. However, I have a situation in which it would be very convenient for my Django app to update the Mongo database of an entirely different app that is running a different server, etc. How can I do this?
I dont know what Django is or how you are talking to Postgress, but ultimately, it boils down to using one of the MongoDB supported language drivers in order to communicate with MongoDB. The programming languages supported are here: drivers
One option to consider is MongoEngine, an Object-Document Mapper (ODM) that implements a declarative API similar to Django. You can then use Django's support for multiple databases to route requests appropriately.
See:
Django Support in MongoEngine
Django documentation: Multiple databases

In Django, how do I make a model that queries my legacy SQL Server and Oracle databases and sends that to the view?

I have a lot of old vbscript webpages using Classis ASP. Those ASP pages have lots of different databases queries to different databases all inside a given .asp file. I want to write those in Python and use Django as the framework. Will I be able to do this? I'm not sure how to start after I install Django. Sure, I can make a demo work, but that's not what I'm after. I will use the normal database "things" in MySQL or PostgreSQL, but sooner or later I have to hit those other databases and bring them back into Django, using Django's templating and so on.
Will I be able to do this? How do I make a model that queries my legacy SQL Server and Oracle database and send that to the view? Am I "fighting the framework" to accomplish this?
Just to be clear. I am not interested in messing with the stock database that Django uses for it's settings. That can stay as it is. I want to use that part for plugins, security (ldap), etc.
Thanks.
You can query different databases in your ORM calls by leveraging the using statement: https://docs.djangoproject.com/en/1.5/ref/models/querysets/#using
This would allow you to set up as many database definitions as you need in settings.py, then specify which DB to query at the view level. That way, you wouldn't have to change your model definition should you decide to consolidate your databases, etc.
Have you reviewed the Django multiple databases documentation?
Django has a built-in Oracle back end, so that should be fairly straightforward.
SQL Server can work through django-pyodbc but I found it fairly painful to set up. If you already have a generally working ODBC connection to your legacy SQL Server database connection from your Django environment it's no big deal, but it took me some trouble to get things set up to where I could use tsql to connect to my SQL Server database.
I also had some trouble with Unicode data from the SQL Server database until I forced it to use the appropriate (later) version of TDS. I just needed it for a script or two, so I set the 'TDSVER' environment variable and left it at that, but in theory there are other places you can set that.

How to use multiple databases in a django application

I need to connect to multiple databases in my django application. I want to make connections to all the databases when ever I start the application and use those connections for all the requests. But I did not understand how to do that (setting these databases connections in global environment).
I tried to set database connections in settings.py and i tried to access those connections in my views using from django.conf import settings, But it is creating new database connections when ever new request comes.
Because of this my website always gives too many database connections error.
Can you please help me how to set these mysql connections in global environment?
Not only is this a planned feature, but one of this years GSOC projects is for multi database support. Alex Gaynor is working on it with Russell Keith-Magee as mentor. So I'm sure the result will be good.
The description of the project:
Django current has the low level hooks necessary for multiple database support,
but it doesn't have the high level API for using, nor any support
infrastructure, documentation, or tests. The purpose of this project would be
to implement the high level API necessary for the use of multiple databases in
Django, along with requisit documentation and tests.
The Django ORM does not support multiple databases.
There are some patches to modify Django with multiple database support called django-multidb You may have some luck with these, although they are a rather old.
Multiple DB support for Django is a planned feature.