将Django与遗留数据库集成不起作用

时间:2016-03-21 09:14:10

标签: python mysql django

每一个:我正在尝试使用django 1.8,mysql db来练习将Django与遗留数据库集成,但它没有成功,任何人都能告诉我发生了什么?非常感谢你! 我的旧数据库

  

models.py

# -*- coding: utf-8 -*-
from django.db import models
from django.utils import timezone


class blog(models.Model):
    name = models.CharField(max_length=255)
    description = models.TextField()
    slug = models.SlugField(unique=True)
    date_time = models.DateTimeField(auto_now_add = True)

    def __unicode__(self):
        return self.name



def get_image_path(instance, filename):
    return '/'.join(['blog_images', instance.bupimg.slug, filename])

class Upload(models.Model):
    bupimg = models.ForeignKey(blog, related_name="uploads")
    image = models.ImageField(upload_to=get_image_path)

我只关注doc here

输入命令后

enter image description here

为什么我的pidapp / models.py显示出与旧数据库的models.py

非常不同
  

pidapp / models.py

# 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, modify, 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 sqlcustom [app_label]'

# into your database.

from __future__ import unicode_literals

from django.db import models


class AuthGroup(models.Model):
    name = models.CharField(unique=True, max_length=80)

    class Meta:
        managed = False
        db_table = 'auth_group'


class AuthGroupPermissions(models.Model):
    group = models.ForeignKey(AuthGroup)
    permission = models.ForeignKey('AuthPermission')

    class Meta:
        managed = False
        db_table = 'auth_group_permissions'
        unique_together = (('group', 'permission'),)


class AuthPermission(models.Model):
    name = models.CharField(max_length=255)
    content_type = models.ForeignKey('DjangoContentType')
    codename = models.CharField(max_length=100)

    class Meta:
        managed = False
        db_table = 'auth_permission'
        unique_together = (('content_type', 'codename'),)


class AuthUser(models.Model):
    password = models.CharField(max_length=128)
    last_login = models.DateTimeField(blank=True, null=True)
    is_superuser = models.IntegerField()
    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=254)
    is_staff = models.IntegerField()
    is_active = models.IntegerField()
    date_joined = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'auth_user'


class AuthUserGroups(models.Model):
    user = models.ForeignKey(AuthUser)
    group = models.ForeignKey(AuthGroup)

    class Meta:
        managed = False
        db_table = 'auth_user_groups'
        unique_together = (('user', 'group'),)


class AuthUserUserPermissions(models.Model):
    user = models.ForeignKey(AuthUser)
    permission = models.ForeignKey(AuthPermission)

    class Meta:
        managed = False
        db_table = 'auth_user_user_permissions'
        unique_together = (('user', 'permission'),)


class BloggingBlog(models.Model):
    name = models.CharField(max_length=255)
    description = models.TextField()
    slug = models.CharField(unique=True, max_length=50)
    date_time = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'blogging_blog'


class BloggingUpload(models.Model):
    bupimg = models.ForeignKey(BloggingBlog)
    image = models.CharField(max_length=100)

    class Meta:
        managed = False
        db_table = 'blogging_upload'


class DjangoAdminLog(models.Model):
    action_time = models.DateTimeField()
    object_id = models.TextField(blank=True, null=True)
    object_repr = models.CharField(max_length=200)
    action_flag = models.SmallIntegerField()
    change_message = models.TextField()
    content_type = models.ForeignKey('DjangoContentType', blank=True, null=True)
    user = models.ForeignKey(AuthUser)

    class Meta:
        managed = False
        db_table = 'django_admin_log'


class DjangoContentType(models.Model):
    app_label = models.CharField(max_length=100)
    model = models.CharField(max_length=100)

    class Meta:
        managed = False
        db_table = 'django_content_type'
        unique_together = (('app_label', 'model'),)


class DjangoMigrations(models.Model):
    app = models.CharField(max_length=255)
    name = models.CharField(max_length=255)
    applied = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'django_migrations'


class DjangoSession(models.Model):
    session_key = models.CharField(primary_key=True, max_length=40)
    session_data = models.TextField()
    expire_date = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'django_session'


class RegistrationRegistrationprofile(models.Model):
    activation_key = models.CharField(max_length=40)
    user = models.ForeignKey(AuthUser, unique=True)
    activated = models.IntegerField()

    class Meta:
        managed = False
        db_table = 'registration_registrationprofile'

我认为pidapp / models.py应该与旧数据库的models.py

相同

1 个答案:

答案 0 :(得分:4)

你做的事没有意义。你已经有了models.py,为什么要创建一个新的? A"遗留数据库"在此上下文中指的是在Django之外创建的数据库,因此没有models.py。绝对没有理由在已经定义了模型的数据库上运行inspectdb。

(另外,生成的模型是正确的,无论如何;该文件包含数据库中所有现有表的模型,包括博客表,还包括所有其他Django表。再次,没有点在这里运行inspectdb。)