【问题标题】:Django Rest Framework MySQL Tables renamed/add prefixDjango Rest Framework MySQL 表重命名/添加前缀
【发布时间】:2018-08-26 09:11:02
【问题描述】:

我是 Django Rest Framework 的初学者,今天遇到了模型和MySQL dabase 的问题。

我有一个这样的 MySQL 表: Mysql View:

models.py:

from django.db import models


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

目录结构:

Structure of Catalogs

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'
]

为什么当我将数据迁移到数据库时,会在表名中添加一个前缀,例如:testsite_

结论:

我想创建 models.py 并将数据迁移到没有此前缀 testsite_ 的 mysql 数据库,它在第一张图片中的情况如何:inndb.testsite_reservation

我该怎么做?

【问题讨论】:

    标签: mysql django django-rest-framework


    【解决方案1】:

    来自 django documentation

    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'
    

    【讨论】:

    • 这很好用,也解决了我在这篇文章中没有提到的另一个问题。谢谢!
    猜你喜欢
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 2020-10-07
    • 1970-01-01
    相关资源
    最近更新 更多