【问题标题】:Why is django not making migrations for my models为什么 django 不为我的模型进行迁移
【发布时间】:2021-10-06 15:22:31
【问题描述】:

我将我的 django 应用程序上传到 pythonanywhere,它可以与 sqlite 数据库一起使用, 但是在切换到 mysql 时它不会进行迁移,但是当我朗姆酒时它会迁移管理表python manage.py migrate

这是我的设置应用

import os
from pathlib import Path

import environ
env = environ.Env(
    DEBUG=(bool, False)
)
environ.Env.read_env()

DEBUG = env('DEBUG')
BASE_DIR = Path(__file__).resolve().parent.parent


# Raises django's ImproperlyConfigured exception if SECRET_KEY not in os.environ
SECRET_KEY = env('SECRET_KEY')

if DEBUG:
    ALLOWED_HOSTS = []

else:
    ALLOWED_HOSTS = [
        '*',
    ]



INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'main',
    'projects',
    'services',

    'crispy_forms',
    'crispy_bootstrap5',
    # should be at bottom
    'django_cleanup.apps.CleanupConfig',
]

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 = 'vectorapp.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / '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 = 'vectorapp.wsgi.application'


if not DEBUG:
    DATABASES = {
        'default': {
            'ENGINE': os.getenv("DB_ENGINE"),
            'NAME': os.getenv("DB_NAME"),
            'USER': os.getenv("DB_USER"),
            'PASSWORD': os.getenv("DB_PASSWORD"),
            'HOST': os.getenv("DB_HOST"),
            # 'PORT': os.getenv("DB_PORT"),
            "OPTIONS": {
                'init_command': "SET sql_mode='STRICT_TRANS_TABLES', innodb_strict_mode=1",
                'charset': 'utf8mb4',
                "autocommit": True,
            }
        }
    }
else:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': BASE_DIR / 'db.sqlite3',
        }
    }


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
LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5"
CRISPY_TEMPLATE_PACK = "bootstrap5"


STATIC_URL = '/static/'

MEDIA_URL = '/media/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]

# STATIC_ROOT = 'static_root'

MEDIA_ROOT = os.path.join(BASE_DIR, 'static/media/')


DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'


if not DEBUG:
    DEFAULT_FILE_STORAGE = 'backend.storage.AzureMediaStorage'
    STATICFILES_STORAGE = 'backend.storage.AzureStaticStorage'
    SECRET_KEY = env("SECRET_KEY")
    AZURE_ACCOUNT_NAME = env("AZURE_ACCOUNT_NAME")
    AZURE_CONNECTION_STRING = env("AZURE_CONNECTION_STRING")
    AZURE_ACCOUNT_KEY = env("AZURE_ACCOUNT_KEY")
    AZURE_URL_EXPIRATION_SECS = int(env("AZURE_URL_EXPIRATION_SECS"))
    # custom
    AZURE_MEDIA_CONTAINER = env("AZURE_MEDIA_CONTAINER")
    AZURE_STATIC_CONTAINER = env("AZURE_STATIC_CONTAINER")

我在这里做错了什么? 我使用了各种数据库,但它仍然说当我尝试迁移时没有检测到任何变化

【问题讨论】:

  • 更改数据库设置不会更改迁移,因为这些迁移是以“方言不变风格”进行的。
  • 尝试在数据库中用 msql 替换 sqlite

标签: python django mysql-python django-migrations pythonanywhere


【解决方案1】:

如Django Migration Docs中提到的:

每个应用程序的迁移文件位于该应用程序内部的“迁移”目录中,旨在提交到其代码库并作为其代码库的一部分进行分发。您应该在您的开发机器上进行一次迁移,然后在您同事的机器、暂存机器以及最终的生产机器上运行相同的迁移。

所以看起来您描述的内容是正常的,因为您已经将迁移连同代码一起提交到服务器。你可以运行

python manage.py showmigrations

在 shell 中查看已应用的内容

【讨论】:

    猜你喜欢
    • 2020-11-10
    • 2016-10-25
    • 2017-06-26
    • 1970-01-01
    • 2015-02-12
    • 2020-11-08
    • 2016-11-17
    • 1970-01-01
    • 2020-08-14
    相关资源
    最近更新 更多