Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple Databases in Django 1.0.2 with custom manager
    primarykey
    data
    text
    <p>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 <a href="http://groups.google.com/group/django-developers/browse_thread/thread/5d8c73eb483029af/19b19ed79bbbfa0d" rel="nofollow noreferrer">here</a> and <a href="http://www.djangrrl.com/view/multiple-database-connection-a-simple-use-case/" rel="nofollow noreferrer">here</a> but have had no luck. I get an empty tuple when returning <code>MyCustomModel.objects.all()</code>. </p> <p>Here is what I have in manager.py </p> <pre><code>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 </code></pre> <p>and here is what I have in models.py: </p> <pre><code>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 </code></pre> <p>But if I run <code>MyCustomModel.objects.all()</code> I get an empty list. </p> <p>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. </p> <p>UPDATE: This now in Django trunk and will be part of the 1.2 release <a href="http://docs.djangoproject.com/en/dev/topics/db/multi-db/" rel="nofollow noreferrer">http://docs.djangoproject.com/en/dev/topics/db/multi-db/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload