Django - 使用inspectdb时清空数据库

时间:2014-06-23 14:46:52

标签: 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中显示为空。当我将Bonds.objects.all()键入shell时,它返回一个空列表,当应该有足够的时候(我已经确认了)。

# 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 个答案:

答案 0 :(得分:2)

您是否遵循了其他指示?您需要将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)将您的应用添加到INSTALLED_APPS

settings.py中,将您的应用添加到INSTALLED_APPS。在此示例中,您希望将'legacy',添加到INSTALLED_APPS

4)运行syncdb

运行python manage.py syncdb以确保包含所有必需的表。看起来你已经有了基于上面输出的输出的那些,但再次运行它会不会受到伤害。

答案 1 :(得分:0)

我找到了答案,这显而易见:数据库路由。您需要创建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/

您需要在in_db = 'desired-db'的每个班级的Meta类中添加名为models.py的属性。