django配合mysql自定義後臺管理系統
阿新 • • 發佈:2019-02-18
建立django專案後,建立元件manage
安裝pymysql,pip install pymysql
並修改檔案__init__.py
import pymysql
- pymysql.install_as_MySQLdb()
找到setting.py,修改設定
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'testchar', 'USER': 'root', 'PASSWORD': '', 'HOST': 'localhost', 'PORT': '3306', } }
要從原有mysql資料庫得到models,使用命令
manage.py inspectdb
得到 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_id = models.IntegerField() permission_id = models.IntegerField() classMeta: managed = False db_table = 'auth_group_permissions' unique_together = (('group_id', 'permission_id'),) class AuthPermission(models.Model): name = models.CharField(max_length=255) content_type_id = models.IntegerField() codename = models.CharField(max_length=100) classMeta: managed = False db_table = 'auth_permission' unique_together = (('content_type_id', '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=150) first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=150) 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_id = models.IntegerField() group_id = models.IntegerField() class Meta: managed = False db_table = 'auth_user_groups' unique_together = (('user_id', 'group_id'),) class AuthUserUserPermissions(models.Model): user_id = models.IntegerField() permission_id = models.IntegerField() class Meta: managed = False db_table = 'auth_user_user_permissions' unique_together = (('user_id', 'permission_id'),) 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.PositiveSmallIntegerField() change_message = models.TextField() content_type_id = models.IntegerField(blank=True, null=True) user_id = models.IntegerField() 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 Journal(models.Model): name = models.CharField(max_length=20) project = models.ForeignKey('Project', models.DO_NOTHING, blank=True, null=True) author = models.CharField(max_length=20) content = models.CharField(max_length=1000) date = models.DateField() open = models.IntegerField() address = models.CharField(max_length=500, blank=True, null=True) stage = models.IntegerField(blank=True, null=True) examine = models.IntegerField(blank=True, null=True) class Meta: managed = False db_table = 'journal' class Project(models.Model): name = models.CharField(max_length=20) introduce = models.CharField(max_length=50) concrete = models.CharField(max_length=5000) could = models.IntegerField() releasedate = models.DateField(db_column='releaseDate') # Field name made lowercase. closingdate = models.DateField(db_column='closingDate') # Field name made lowercase. applicationdate = models.DateField(db_column='applicationDate') # Field name made lowercase. class Meta: managed = False db_table = 'project' class User(models.Model): name = models.CharField(unique=True, max_length=20) number = models.IntegerField() email = models.CharField(max_length=20) phone = models.CharField(max_length=20) password = models.CharField(max_length=20) type = models.IntegerField() class Meta: managed = False db_table = 'user'
貼上到models.py
找到admin.py,註冊模型
from manage.models import Journal,Project,User; # Register your models here. admin.site.register([Journal,Project,User])然後遷移資料,使用django自帶的後臺管理系統來管理本專案的資料庫