2

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:

  • Database 1: Django Auth / Admin (Full access)
  • Database 2: My First Database (Scripts) (only read/write access: no table creation)
  • Database 3: My Second Database (Service) (only read/write access: no table creation)

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?

1
  • This is an import error, so the problem is probably naming - I'm guessing it's seeing 'ticket.DataRouter' and doing from ticket import DataRouter. Instead, put all your routers in a routers.py file and then do DATABASE_ROUTERS = ['ticket.routers.DataRouter', 'ticket.routers.ScriptRouter'] Commented Dec 28, 2014 at 9:31

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

I agree with you that I need to look further into routers, but how does a router determine which database to use? Especially as there may be times where I may want to access both databases in a given application. The docs show very clearly how to direct a single app to a database, or how to randomly select a database, but I may need that routing based on the table name itself.
you can link a specific model to a database instead of an entire app, though I never tried doing anything of the sort

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.