cyram
cyram

Reputation: 840

Django Interface with Multiple Databases (but not migrating)

I am new to Django, and so it has been a bit of a tutorial-fest these past few weeks. I think I understand the basics, and have enjoyed making a lot of simple things (blogs, photo galleries, calendars, etc) from the various tutorials. I still feel very much a beginner with Django, but am now trying to customise it to fit my project's needs.

Situation: I have the task of creating an interface for two databases. The interface's main purpose is to put a view on the complex relationships between tables, but also edit them if needed. My problem is that I have basic read/write access to my two databases, but cannot do things like create tables. Because of this, creating a model and sync/migrating these databases with Django isn't possible since it wants to create tables onto these databases in order to do it. My efforts so far have been to focus on getting the following routing/model structure:

The closest tutorial I have found that addresses what I want is this one: Reporting Django Multi DB Support, but my two databases don't have easily separable prefixes in their table definitions and so I'm not sure of how to write the router definition file short of writing conditionals for each and every Table in each database in the db_for_read/db_for_write functions.

I'm beginning to feel that I am trying to get Django to do something that it really doesn't want to do.

So my question is this: Do I continue to try to get Django to do what I want? If so, then does anyone have any targeted resources, tutorials or advice that could help me? I've already read through the official docs, but they don't seem to target my problem.

If I don't go with Django, I've considered something like Flask since it has a bit more freedom. My project, however, will end up being a rather large high-volume website with multiple projects and apps included. My experience with Flask has been great, but I worry that using it for such a large scale project would result in a bit of a mess if I'm not careful.

Any suggestions?

UPDATE: I have progressed a bit (in part to yuvi's suggestion below) but I still have an error. It is now reading from the router that I want, but not correctly. Here are some of the files in question:

settings.py

DATABASE_ROUTERS = ['ticket.DataRouter', 'ticket.ScriptRouter']

ScriptModel.py

from __future__ import unicode_literals
from django.db import models

class Archiveticket(models.Model):
    ....

ScriptRouter.py

class ScriptRouter(object):
    def db_for_read(self, model, **hints):
        return 'Scripts'
    def db_for_read(self, model, **hints):
        return 'Scripts'
    def allow_relation(self, obj1, obj2, **hints):
        return None
    def allow_migrate(self, db, model):
        return False

DataRouter.py

class DataRouter(object):
    def db_for_read(self, model, **hints):
        return 'Data'
    def db_for_read(self, model, **hints):
        return 'Data'
    def allow_relation(self, obj1, obj2, **hints):
        return None
    def allow_migrate(self, db, model):
        return False      

Views.py

from django.http import HttpResponse
from django.shortcuts import render

from django.db import models
from ticket.ScriptModel import Archiveticket

def index(request):
    a = Archiveticket.objects.all()
    return render(request, 'tickets.tmpl', {'obj' : a})

tickets.tmpl

{% for b in obj %}
{{ b.archiveTicketId }}<br/>
{% endfor %}

I am getting the error when I load the URL:

ImportError Module "ticket.DataRouter" does not define a "DataRouter" attribute / class

But, if I look at the DataRouter.py file, the class is definitely specified. I must have missed something here. I know the DataRouter is first as that is where most queries will be made, but as a POC, I wanted to query the other table, and it seems to fail when looking at the first database. What am I missing?

Upvotes: 2

Views: 1982

Answers (1)

yuvi
yuvi

Reputation: 18457

You probably want to look into setting up your own routers to control the multiple databases:

The easiest way to use multiple databases is to set up a database routing scheme. The default routing scheme ensures that objects remain ‘sticky’ to their original database [...]

However, if you want to implement more interesting database allocation behaviors, you can define and install your own database routers.

Essentially, you configure a FooRoute class with four methods: db_for_read, db_for_write, allow_relation and allow_migrate, and then you register them in settings.py:

DATABASE_ROUTERS = ['path.to.FooRouter']

See the docs for a detailed example

Upvotes: 1

Related Questions