如何访问Django中的数据,这些模型没有在models.py中定义? [英] How to access data in Django which models is not defined in models.py?

查看:377
本文介绍了如何访问Django中的数据,这些模型没有在models.py中定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已正确配置数据库。如果我想从数据库 genes 中访问表中的数据 hello 。什么是正确的方法呢?我不能从 books.models import hello 做,因为我在models.py没有hello。数据库基因和表hello不是默认的Django数据库。我有两个数据库。我已经设置路由器。我现在要访问数据。我怎样才能做到这一点?感谢

解决方案

您需要做几件事。



settings.py

  DATABASES = {
'genes':{
'ENGINE':'django。 db.backends.mysql',
'NAME':'genes',
'USER':'root',
'PASSWORD':'root',
'HOST' :'localhost',
'PORT':'3360',
},
'default':{
'ENGINE':'django.db.backends.mysql'
'NAME':'django',
'USER':'root',
'PASSWORD':'root',
'HOST':'localhost',
'PORT':'3360',
}
}

DATABASE_ROUTERS = ['genes.routers.GeneRouter',]



routes.py

  class GeneRouter ):
一个路由器控制
中模型上的所有数据库操作基因应用程序

def db_for_read(self,model,** hints):
将基因模型上的所有操作指向基因
如果model._meta.app_label =='genes':
返回'remrate'
返回无
b $ b def db_for_write(self,model,** hints):
将基因模型上的所有操作都指向'genes'
如果model._meta.app_label =='genes':
return'remrate'
return None

def allow_syncdb(self,db,model):
确保基因应用程序只出现在'genes'db
if model._meta.app_label in ['south']:
return True
如果db =='remrate':
return model._meta.app_label =='genes'
elif model._meta.app_label =='genes':
return False
return None

有了这个设置,你需要为



models.py


$创建models.py文件b $ b

  class Hello(models.Model):
Hello model

field1 = models.CharField(max_length = 64)

class Meta:
db_table = u'hello'
managed = False

执行此操作..

  python manage.py inspectdb 

完成后 - 您应该可以使用标准Django查询进行查询

  Hello.objects.all()


I have configured database properly. If I want to access the data from the table hello in the database called genes. What's the proper way to do it? I can't do from books.models import hello as I don't have hello in models.py. The database genes and table hello is not the default Django database. I have two databases. I have already setup Router. I want to now access data. How can I do that? Thanks

解决方案

You need to do a few things..

settings.py

DATABASES = {
    'genes': {
        'ENGINE':   'django.db.backends.mysql',
        'NAME':     'genes',
        'USER':     'root',
        'PASSWORD': 'root',
        'HOST':     'localhost',
        'PORT':     '3360',
    },
    'default': {
        'ENGINE':   'django.db.backends.mysql',
        'NAME':     'django',
        'USER':     'root',
        'PASSWORD': 'root',
        'HOST':     'localhost',
        'PORT':     '3360',
    }
}

DATABASE_ROUTERS = ['genes.routers.GeneRouter',]

routers.py

class GeneRouter(object):
    """A router to control all database operations on models in
    the genes application"""

    def db_for_read(self, model, **hints):
        "Point all operations on genes models to 'genes'"
        if model._meta.app_label == 'genes':
            return 'remrate'
        return None

    def db_for_write(self, model, **hints):
        "Point all operations on genes models to 'genes'"
        if model._meta.app_label == 'genes':
            return 'remrate'
        return None

    def allow_syncdb(self, db, model):
        "Make sure the genes app only appears on the 'genes' db"
        if model._meta.app_label in ['south']:
            return True
        if db == 'remrate':
            return model._meta.app_label == 'genes'
        elif model._meta.app_label == 'genes':
            return False
        return None

One you have this set up then you need to create the models.py file for the

models.py

class Hello(models.Model):
    """Hello model"""

    field1 = models.CharField(max_length=64)

    class Meta:
        db_table = u'hello'
        managed = False

To do this progmatically..

python manage.py inspectdb

Once this is done - you should be able to Query using standard Django querysets

Hello.objects.all()

这篇关于如何访问Django中的数据,这些模型没有在models.py中定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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