我是 Django Rest Framework 的初学者,今天我在模型和 dabase MySQL
遇到了问题。
我有一个这样的 MySQL 表:Mysql 视图:
models.py:
from django.db import models
class Reservation(models.Model):
reservations = models.CharField(max_length=200)
done = models.BooleanField()
目录结构:
目录结构
Settings.py
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'x'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = [
'localhost',
]
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'testsite',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'testsite.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'testsite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'innodb', # Or path to database file if using sqlite3.
'USER': 'webroot', # Not used with sqlite3.
'PASSWORD': os.getenv('MYSQL_PASSWORD'), # Not used with sqlite3.
'HOST': 'xxxx.us-east-1.rds.amazonaws.com', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '3306', # Set to empty string for default. Not used with sqlite3.
}
}
# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
'static'
]
为什么当我将数据迁移到 dabase 时,有些东西会在表名中添加前缀,例如:testsite_
结论:
我想创建 models.py 并将数据迁移到没有这个前缀的 mysql datebase testsite_
,它在第一个图像中的情况:inndb.testsite_reservation
我该怎么做?
来自 django 文档
Django 自动从 模型类的名称以及包含该类的应用。模型的 数据库表名称是通过连接模型的"应用标签"来构造的 – 您在
manage.py startapp
中使用的名称 – 模型的类名, 它们之间带有下划线。要覆盖数据库表名称,请使用 中的
db_table
参数class Meta
.
所以对于你的例子,你可以写
class Reservation(models.Model):
reservations = models.CharField(max_length=200)
done = models.BooleanField()
class Meta:
db_table='testsite_reservation'