开发者

django mulitidb

Hi i am using django 1.2 .I my case i have few models which are present in one db and others on second db.I have changed my settings.py file with the two database details

But when i run the server it says rel开发者_Go百科ation not exist.

How can i tell that for this model refer 'a' database and for others refer 'b' database.

#models.py

### this class is in a database #####


class Test(models.Model):
   id=models.AutoField(primary_key=True)
   ...
   ...
   ...
   ...
   class Meta:
      app_label=ugettext('Test')
      db_table='testing'
      verbose_name=ugettext_lazy('Testing 1,2')
   def __unicode__(self):
       return self.name


 ### this class is in b database #####


 class Test2(modes.Model):
    id=models.AutoField(primary_key=True)
    name=models.CharField(max_length=200)
    ...
    ....
    ....
    class Meta:
      app_label=ugettext('Test')
      db_table='testing2'
      verbose_name=ugettext_lazy('1,2')
    def __unicode__(self):
       return self.name

  # settings.py
   DATABASES= {  'default': {
          'NAME': 'a',
          'ENGINE': 'django.db.backends.postgresql_psycopg2',
          'USER': 'asdf',
          'PASSWORD': '123'

         },
        'users': {
          'NAME': 'b',
      'ENGINE': 'django.db.backends.postgresql_psycopg2',
      'USER': 'asdf',
      'PASSWORD': '123'

  }
 }


The django documentation has a special page for multi-db related things:

  • http://docs.djangoproject.com/en/dev/topics/db/multi-db/

For example, a verbose way would be to specify the database to use, when a query is made, see:

  • http://docs.djangoproject.com/en/dev/topics/db/multi-db/#manually-selecting-a-database-for-a-queryset

For a general approach, you should specify some DATABASE_ROUTERS:

  • http://docs.djangoproject.com/en/dev/topics/db/multi-db/#using-routers
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜