tags:

views:

543

answers:

3

I asked this in the users group with no response so i thought I would try here. I am trying to setup a custom manager to connect to another database on the same server as my default mysql connection. I have tried following the examples here and here but have had no luck. I get an empty tuple when returning MyCustomModel.objects.all().

Here is what I have in manager.py

from django.db import models 
from django.db.backends.mysql.base import DatabaseWrapper 
from django.conf import settings 
class CustomManager(models.Manager): 
    """ 
    This Manager lets you set the DATABASE_NAME on a per-model basis. 
    """ 
    def __init__(self, database_name, *args, **kwargs): 
        models.Manager.__init__(self, *args, **kwargs) 
        self.database_name = database_name 
    def get_query_set(self): 
        qs = models.Manager.get_query_set(self) 
        qs.query.connection = self.get_db_wrapper() 
        return qs 
    def get_db_wrapper(self): 
        # Monkeypatch the settings file. This is not thread-safe! 
        old_db_name = settings.DATABASE_NAME 
        settings.DATABASE_NAME = self.database_name 
        wrapper = DatabaseWrapper() 
        wrapper._cursor(settings) 
        settings.DATABASE_NAME = old_db_name 
        return wrapper 

and here is what I have in models.py:

from django.db import models 
from myproject.myapp.manager import CustomManager 
class MyCustomModel(models.Model): 
    field1  = models.CharField(max_length=765) 
    attribute = models.CharField(max_length=765) 
    objects = CustomManager('custom_database_name') 
    class Meta: 
        abstract = True 

But if I run MyCustomModel.objects.all() I get an empty list.

I am pretty new at this stuff so I am not sure if this works with 1.0.2, I am going to look into the Manager code to see if I can figure it out but I am just wondering if I am doing something wrong here.

UPDATE: This now in Django trunk and will be part of the 1.2 release http://docs.djangoproject.com/en/dev/topics/db/multi-db/

A: 

This probably isnt the answer your looking for, but its probably best if you move everything you need into the one database.

Thanks spence, that's not really an option becuase I want to access the data from other applications like freeradius.
Justin Hamade
+1  A: 

My company has had success using multiple databases by closely following this blog post: http://www.eflorenzano.com/blog/post/easy-multi-database-support-django/

Bluu
Thanks Bluu I think I saw this before but not sure if I tried it or not. My app is working with some helper methods that have their own mysql connection so this will have to wait for v2 I guess.
Justin Hamade
+4  A: 

You may want to speak to Alex Gaynor as he is adding MultiDB support and its pegged for possible release in Django 1.2. I'm sure he would appreciate feedback and input from those that are going to be using MultiDB. There is discussions about it in the django-developers mainling list. His MultiDB branch may even be useable, I'm not sure.

Since I guess you probably can't wait and if the MultiDB branch isn't usable, here are your options.

  • Follow Eric Flows method, bearing in mind that its not supported and new released of Django may break it. Also, some comments suggest its already been broken. This is going to be hacky.
  • Your other option would be to use a totally different database access method for one of your databases. Perhaps SQLAlchemy for one and then Django ORM. I'm going by the guess that one is likely to be more Django centric and the other is a legacy database.

To summarise. I think hacking MultiDB into Django is probably the wrong way to go unless your prepared to keep up with maintaining your hacks later on. Therefore I think another ORM or database access would give you the cleanest route as then you are not going out with supported features and at the end of the day, its all just Python.

Orange Box
Looks like this has been commited to trunk now and has docs available too http://docs.djangoproject.com/en/dev/topics/db/multi-db/
Justin Hamade
Yup. It sure has and its awesome :D
Orange Box