Joe*_*Lin 1 python mysql django
每个人:我正在尝试使用 django 1.8,mysql db 练习将 Django 与旧数据库集成,但没有成功,谁能告诉我发生了什么?非常感谢!我的 旧数据库
模型.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)
Run Code Online (Sandbox Code Playgroud)
为什么我的 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'
Run Code Online (Sandbox Code Playgroud)
我认为 pidapp/models.py 应该与旧数据库的 models.py 相同
你在做什么毫无意义。您已经有了一个models.py,为什么还要尝试创建一个新的?在这种情况下,“遗留数据库”指的是在 Django 之外创建的数据库,因此没有 models.py。绝对没有理由在已经定义了模型的数据库上运行检查数据库。
(另外,无论如何,生成的模型是正确的;该文件包含数据库中所有现有表的模型,包括博客表,还包括所有其他 Django 表。同样,在这里运行 inspectdb毫无意义。)