Django -- 使用inspectdb时数据库为空
我正在按照这个文档进行操作:https://docs.djangoproject.com/en/1.6/howto/legacy-databases/
首先,我把数据库添加到了settings.py文件里,然后我输入了
python manage.py inspectdb
这是输出的结果。抱歉内容有点长,希望你只需要关注大概的情况。
我遇到的问题是:
1) 我不知道怎么往数据库里放东西(用文本编辑器打开数据库时看到的都是随机字符)
2) 如果我运行python manage.py sqlcustom [应用名称],什么都没有输出
3) 可能因为以上两个问题,导入的数据库在Django中显示为空。当我在命令行输入Bonds.objects.all()时,它返回的是一个空列表,而实际上应该有很多数据(我已经确认过了)。
# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
# * Rearrange models' order
# * Make sure each model has one field with primary_key=True
# * Remove `managed = False` lines if you wish to allow Django to create and delete the table
# Feel free to rename the models, but don't rename db_table values or field names.
#
# Also note: You'll have to insert the output of 'django-admin.py sqlcustom [appname]'
# into your database.
from __future__ import unicode_literals
from django.db import models
class AuthGroup(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(unique=True, max_length=80)
class Meta:
managed = False
db_table = 'auth_group'
class AuthGroupPermissions(models.Model):
id = models.IntegerField(primary_key=True)
group_id = models.IntegerField()
permission = models.ForeignKey('AuthPermission')
class Meta:
managed = False
db_table = 'auth_group_permissions'
class AuthPermission(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=50)
content_type_id = models.IntegerField()
codename = models.CharField(max_length=100)
class Meta:
managed = False
db_table = 'auth_permission'
class AuthUser(models.Model):
id = models.IntegerField(primary_key=True)
password = models.CharField(max_length=128)
last_login = models.DateTimeField()
is_superuser = models.BooleanField()
username = models.CharField(unique=True, max_length=30)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.CharField(max_length=75)
is_staff = models.BooleanField()
is_active = models.BooleanField()
date_joined = models.DateTimeField()
class Meta:
managed = False
db_table = 'auth_user'
class AuthUserGroups(models.Model):
id = models.IntegerField(primary_key=True)
user_id = models.IntegerField()
group = models.ForeignKey(AuthGroup)
class Meta:
managed = False
db_table = 'auth_user_groups'
class AuthUserUserPermissions(models.Model):
id = models.IntegerField(primary_key=True)
user_id = models.IntegerField()
permission = models.ForeignKey(AuthPermission)
class Meta:
managed = False
db_table = 'auth_user_user_permissions'
class Bonds(models.Model):
bond_id = models.TextField(blank=True)
end_d = models.DateField(blank=True, null=True)
intr = models.FloatField(blank=True, null=True)
base_i = models.FloatField(blank=True, null=True)
type = models.TextField(blank=True)
start_d = models.DateField(blank=True, null=True)
first_id = models.DateField(blank=True, null=True)
first_pd = models.DateField(blank=True, null=True)
class Meta:
managed = False
db_table = 'bonds'
class Combos(models.Model):
type = models.TextField(blank=True)
f_prop = models.FloatField(blank=True, null=True)
f_start = models.IntegerField(blank=True, null=True)
f_end = models.IntegerField(blank=True, null=True)
b_prop = models.FloatField(blank=True, null=True)
b_start = models.IntegerField(blank=True, null=True)
b_end = models.IntegerField(blank=True, null=True)
a_prop = models.FloatField(blank=True, null=True)
a_start = models.IntegerField(blank=True, null=True)
a_end = models.IntegerField(blank=True, null=True)
class Meta:
managed = False
db_table = 'combos'
class DjangoAdminLog(models.Model):
id = models.IntegerField(primary_key=True)
action_time = models.DateTimeField()
user = models.ForeignKey(AuthUser)
content_type = models.ForeignKey('DjangoContentType', blank=True, null=True)
object_id = models.TextField(blank=True)
object_repr = models.CharField(max_length=200)
action_flag = models.PositiveSmallIntegerField()
change_message = models.TextField()
class Meta:
managed = False
db_table = 'django_admin_log'
class DjangoContentType(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=100)
app_label = models.CharField(max_length=100)
model = models.CharField(max_length=100)
class Meta:
managed = False
db_table = 'django_content_type'
class DjangoSession(models.Model):
session_key = models.CharField(unique=True, max_length=40)
session_data = models.TextField()
expire_date = models.DateTimeField()
class Meta:
managed = False
db_table = 'django_session'
class DjangoSite(models.Model):
id = models.IntegerField(primary_key=True)
domain = models.CharField(max_length=100)
name = models.CharField(max_length=50)
class Meta:
managed = False
db_table = 'django_site'
class FxRates(models.Model):
type = models.TextField(blank=True)
fx_rate = models.FloatField(blank=True, null=True)
class Meta:
managed = False
db_table = 'fx_rates'
class NotendurDocument(models.Model):
id = models.IntegerField(primary_key=True)
docfile = models.CharField(max_length=100)
user = models.ForeignKey(AuthUser)
class Meta:
managed = False
db_table = 'notendur_document'
class Types(models.Model):
type = models.TextField(blank=True)
cal = models.TextField(blank=True) # This field type is a guess.
ind = models.TextField(blank=True)
paypy = models.IntegerField(blank=True, null=True)
loan_type = models.TextField(blank=True)
adj_intr_date = models.NullBooleanField()
class Meta:
managed = False
db_table = 'types'
2 个回答
我找到了答案,其实很简单:就是数据库路由。你需要创建一个 routers.py
文件,然后在你的 settings.py
文件里加上这个内容:
DATABASE_ROUTERS = ['myapp1.routers.name-of-your-routers.py-class',
'myapp2.routers.name-of-your-routers.py-class']
DATABASE_APPS_MAPPING = {'myapp1': 'mydb1',
'myapp2':'mydb2'}
Djangosnippets 网站上有这个 routers.py
文件的例子: https://djangosnippets.org/snippets/2687/
你还需要在每个类的 Meta 类里加一个叫 in_db = 'desired-db'
的属性,这些类都在你的 models.py
文件里。
你有没有按照其他的步骤去做?你需要把 inspectdb
的输出结果添加到你某个应用的 models.py
文件里,然后把这个应用加到你的 INSTALLED_APPS
里。
试试下面的步骤:
1) 创建一个应用
如果你还没有想要放 inspectdb
输出结果的应用,可以运行 python manage.py startapp legacy
。这样会创建一个叫 legacy
的应用,你可以通过它来同步你现有的数据库。
2) 把 inspectdb 的输出添加到你应用的模型文件
在你选择的应用的 models.py
文件中,把 inspectdb
命令的输出粘贴进去。最简单的方法可能是运行 python manage.py inspectdb > models.py
。这会在和 manage.py
以及你的应用目录相同的文件夹里创建一个叫 models.py
的文件。然后把这个文件里的内容复制到你应用的 models.py
文件中,也就是 legacy/models.py
。
3) 把你的应用添加到 INSTALLED_APPS
在你的 settings.py
文件中,把你的应用添加到 INSTALLED_APPS
里。在这个例子中,你需要把 'legacy',
加到 INSTALLED_APPS
中。
4) 运行 syncdb
运行 python manage.py syncdb
来确保所有需要的表都包含在内。根据上面粘贴的输出结果来看,你应该已经有这些表了,但再运行一遍也没坏处。