Django 1.0.2中的多个数据库与自定义管理器 [英] Multiple Databases in Django 1.0.2 with custom manager

查看:142
本文介绍了Django 1.0.2中的多个数据库与自定义管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用户组中询问没有回应,所以我以为我会在这里尝试。
我正在设置一个自定义管理器来连接另一个数据库
在与我的默认mysql连接相同的服务器上。以下示例之后,我尝试过
这里此处,但没有运气。返回
MyCustomModel.objects.all()时,我得到一个空的元组。

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().

以下是我在manager.py中的内容:

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 

这里是我在models.py中有的:

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 

但是,如果我运行 MyCustomModel.objects.all() code>我得到一个空的列表。

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

我在这个东西很新,所以我不知道这是否适用于
1.0.2,我将查看经理代码看看如果我可以计算
,但我只是想知道我是否在这里做错了。

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.

更新:
这个现在在Django中继并将成为1.2版本的一部分
http://docs.djangoproject.com/en/dev/topics/db/multi-db/

推荐答案

你可能想和 Alex Gaynor ,他正在添加MultiDB支持,并将其与 Django 1.2 。我相信他会欣赏那些将要使用MultiDB的反馈和投入。在 django-developers 主菜单中有讨论。他的MultiDB分支甚至可以使用,我不知道。

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.

由于我猜你可能不能等待,如果MultiDB分支是不可用的,这里是您的选项。

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


  • 按照 Eric Flows方法,请记住,它不支持和新发布的Django可能会破坏它。另外,有些评论建议其已经被打破了这将是恶作剧。

  • 您的其他选项将是为您的一个数据库使用完全不同的数据库访问方法。也许 SQLAlchemy 一个,然后是Django ORM。我会猜测,一个可能是更多的Django为中心,另一个是遗留数据库。

  • 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.

总结。我认为将MultiDB劫持到Django可能是错误的方法,除非你准备好跟上维护你的黑客。因此,我认为另一个ORM或数据库访问将为您提供最干净的路由,因为您不会出现支持的功能,而在一天结束时,它只是Python。

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.

这篇关于Django 1.0.2中的多个数据库与自定义管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆