Search code examples
djangolegacymultiple-databases

Adding a second legacy database to an existing django 1.2 project


I have a django project that I have been using for a while, but now I want to add some a new dataset from a second, non-django database. I have successfully configured my settings.py file to use the new database, which is called 'mediawikidb'.

I am able to run the following command:

python manage.py inspectdb --database=mediawikidb

and I get django's model representations for the database, but the table that I am interested in is called 'User'. The output of inspectdb looks like this:

class User(models.Model):
user_id = models.IntegerField(primary_key=True)
user_name = models.CharField(unique=True, max_length=255)
user_real_name = models.CharField(max_length=255)
user_password = models.TextField()
user_newpassword = models.TextField()
user_newpass_time = models.CharField(max_length=14, blank=True)
user_email = models.TextField()
user_options = models.TextField()
user_touched = models.CharField(max_length=14)
user_token = models.CharField(max_length=32)
user_email_authenticated = models.CharField(max_length=14, blank=True)
user_email_token = models.CharField(max_length=32, blank=True)
user_email_token_expires = models.CharField(max_length=14, blank=True)
user_registration = models.CharField(max_length=14, blank=True)
user_editcount = models.IntegerField(null=True, blank=True)
class Meta:
    db_table = u'user'

I have been reading the docs [0] but I am pretty confused so I thought I would ask here after reading around for a while. I can't just drop this in my model, can I? If I do it will cause issues with django for sure. How should I do this?

My other question is, how will django know which database this 'User' model instance relates to? I feel like I am missing something, but I can't find anything that makes sense.

How should I set up my model to be able to access this data without messing up anything in django?

[0] http://docs.djangoproject.com/en/dev/topics/db/multi-db/


Solution

  • Check the following docs:

    http://docs.djangoproject.com/en/dev/topics/db/multi-db/#an-example

    They have an example of a router that indicates where to read data from and write data to. For your user model, you can configure all interactions with the mediawiki database and all other models would use your normal database.

    Just make sure to disable relations...

    class MyAppRouter(object):
    
        def _is_user_model(model):
            return str(model.__name__) == 'User' and model._meta.app_label == 'myapp'
    
        def db_for_read(self, model, **hints):
            if self._is_user_model(model):
                return 'mediawikidb'
            return None
    
        def db_for_write(self, model, **hints):
            if self._is_user_model(model):
                return 'mediawikidb'
            return None
    
        def allow_relation(self, obj1, obj2, **hints):
            if self._is_user_model(obj1) or self._is_user_model(obj2):
                return False
            return None
    
        def allow_syncdb(self, db, model):
            if db == 'mediawikidb':
                return False
            elif self._is_user_model(model):
                return False
            return None