Joe Lin
Joe Lin

Reputation: 633

Integrating Django with a legacy database does not work

every one: I am trying to practice Integrating Django with a legacy database using django 1.8,mysql db, but it did not success,can any one tell me what's happening??thank you very much! my legacy database

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)

and I just follow the doc here

after I type the command enter image description here

why my pidapp/models.py shows very different than the legacy database's 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'

I thought the pidapp/models.py should be same as the legacy database's models.py

Upvotes: 1

Views: 561

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599560

What you're doing makes no sense. You already have a models.py, why are you trying to create a new one? A "legacy db" in this context refers to a database that has been created outside of Django, and so has no models.py. There's absolutely no reason to run inspectdb on a db that already has models defined.

(Plus, the generated models are correct, anyway; the file contains models for all the existing tables in your database, including the blogging ones, but also including all the other Django tables. Again, there is no point in running inspectdb here.)

Upvotes: 5

Related Questions