Django Rest框架MySQL表重命名/添加前缀

Django Rest框架MySQL表重命名/添加前缀,mysql,django,django-rest-framework,Mysql,Django,Django Rest Framework,我是Django Rest框架的初学者,今天我在模型和MySQLdabase方面遇到了问题 我有一个MySQL表,如下所示: 型号。py: from django.db import models class Reservation(models.Model): reservations = models.CharField(max_length=200) done = models.BooleanField() 目录结构: from django.db import m

我是Django Rest框架的初学者,今天我在模型和
MySQL
dabase方面遇到了问题

我有一个MySQL表,如下所示:

型号。py:

from django.db import models


class Reservation(models.Model):
    reservations = models.CharField(max_length=200)
    done = models.BooleanField()
目录结构:

from django.db import models


class Reservation(models.Model):
    reservations = models.CharField(max_length=200)
    done = models.BooleanField()

设置.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\uuu

结论:

我想创建models.py并将数据迁移到mysql数据库,而不使用前缀
testsite\u
,它在第一张图片中的样子:inndb.testsite\u

我该怎么做?

来自django

Django自动从 模型类及其包含的应用程序的名称。模特的 数据库表名是通过加入模型的“应用程序标签”来构造的 –您在
manage.py startapp
中使用的名称–用于模型的类名, 它们之间有一个下划线

要覆盖数据库表名,请使用中的
db\u table
参数
类元

所以你可以写下你的例子

class Reservation(models.Model):
    reservations = models.CharField(max_length=200)
    done = models.BooleanField()
    class Meta:
        db_table='testsite_reservation'
来自django

Django自动从 模型类及其包含的应用程序的名称。模特的 数据库表名是通过加入模型的“应用程序标签”来构造的 –您在
manage.py startapp
中使用的名称–用于模型的类名, 它们之间有一个下划线

要覆盖数据库表名,请使用中的
db\u table
参数
类元

所以你可以写下你的例子

class Reservation(models.Model):
    reservations = models.CharField(max_length=200)
    done = models.BooleanField()
    class Meta:
        db_table='testsite_reservation'

这很好,也解决了我在这篇文章中没有提到的另一个问题。谢谢这很好,也解决了我在这篇文章中没有提到的另一个问题。谢谢