python - Django——使用 inspectdb 时清空数据库

标签 python database django

我正在尝试遵循文档: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。这将在包含 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 以确保包含所有必需的表。看起来您已经有了基于上面粘贴的输出的那些,但再次运行它不会有什么坏处。

关于python - Django——使用 inspectdb 时清空数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24368883/

相关文章:

python - 蒙戈引擎 : database names cannot contain the character '.'

Python 属性错误 : 'NoneType' object has no attribute 'save' when trying to save a cropped image

ios - 在 Cordova iOS 包中找不到 `OBJROOT="$OBJROOT"` 来尝试修复构建错误 : xcodebuild: Command failed with exit code 65

python - django 中的 DISTINCT ON

python - 使用 OpenCV Python 从 Android 智能手机捕获视频

mysql - SQLite查询获取两个维度记录(学生与测验)

sql - 排序字母数字数据

python - 如何从 views.py 测试选中的单选按钮?

Django REST Framework - 获取相关对象

python - 程序调用 : at least one command line parameter