如何访问Django中的数据哪些模型没有在models.py中定义?

时间:2021-05-15 07:34:11

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

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

1 个解决方案

#1


1  

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文件

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

完成后 - 您应该能够使用标准Django查询集进行查询

Hello.objects.all()

#1


1  

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文件

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

完成后 - 您应该能够使用标准Django查询集进行查询

Hello.objects.all()