我正试着跟随医生:https://docs.djangoproject.com/en/1.6/howto/legacy-databases/
首先,我将数据库添加到settings.py中,然后键入
python manage.py inspectdb
这是输出。很抱歉这么长时间了,希望你只需要看大局。
我的问题是
1)我不知道如何将某些内容放入数据库(用文本编辑器打开数据库时只是随机字符)
2)如果运行python manage.py sqlcustom[app name],则不会输出任何内容
3)可能由于上述两个原因,导入的数据库在django中显示为空。当我在shell中键入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'
最佳答案
你照其他的指示走了吗?您需要将inspectdb
的输出添加到某个应用程序的models.py
文件中,然后将该应用程序添加到您的INSTALLED_APPS
中。
请尝试以下操作:
1)创建应用程序
如果您没有要将inspectdb
的输出放入的应用程序,只需运行python manage.py startapp legacy
。这将创建一个名为legacy
的应用程序,可以通过现有的数据库同步现有数据库。
2)将inspectdb输出添加到应用程序的模型文件中
在所选应用程序的models.py
文件中,粘贴inspectdb
命令的输出。最简单的方法可能是运行python manage.py inspectdb > models.py
。这将在具有models.py
和应用程序目录的同一目录中创建一个名为manage.py
的文件。将该文件的输出复制到应用程序中的models.py
文件中,即在legacy/models.py
中。
3)将你的应用添加到已安装的应用
在settings.py
中,将应用程序添加到INSTALLED_APPS
。在本例中,您需要将'legacy',
添加到INSTALLED_APPS
。
4)运行syncdb
运行python manage.py syncdb
以确保包含所有必需的表。看起来您已经有了基于上面粘贴的输出的那些,但是再次运行它不会造成伤害。
关于python - Django —使用inspectdb时为空数据库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24368883/