【问题标题】:django docker db migrations is not working for the new modeldjango docker db migrations 不适用于新模型
【发布时间】:2019-12-19 05:42:58
【问题描述】:

我是django docker 的新人。

我为django 应用程序创建了新模型,我正在开发docker。当我尝试使用迁移命令docker exec -ti 75ce87c91dc7 sh -c "python app/manage.py migrate" 时,它显示:No migrations to apply。这里我已经添加了我的 models.py 文件,我们需要将这个模型导入到其他任何地方吗?

文件夹结构:

settings.py

"""
Django settings for trialriskincApi project.

Generated by 'django-admin startproject' using Django 2.2.7.

For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""

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.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'xh86wu$_k@g46*+y9v_$2q^jnfg$uc44yh4+15nl2+2dx^$il%'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['0.0.0.0']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'trialriskincApi',
]

MIDDLEWARE = [
    'trialriskincApi.middleware.open_access_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 = 'trialriskincApi.urls'

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


# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'trail_risk_inc_backend',
        'USER': 'root',
        'PASSWORD': '12345678',
        'HOST': 'db',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}


# Password validation
# https://docs.djangoproject.com/en/2.2/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.2/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.2/howto/static-files/

STATIC_URL = '/static/'




# Cors headers

CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = False

CORS_ALLOW_HEADERS = [
    'accept',
    'accept-encoding',
    'authorization',
    'content-type',
    'dnt',
    'origin',
    'user-agent',
    'x-csrftoken',
    'x-requested-with',
]

models.py

 from django.conf import settings
    from django.db import models
    from django.utils import timezone
    from django.contrib.auth.models import User

    class  Userprofile(models.Model) :

        user = models.OneToOneField(User, on_delete=models.CASCADE)
        type = models.SmallIntegerField(max_length=1)
        created_date = models.DateTimeField(default=timezone.now)
        updated_date = models.DateTimeField(blank=True, null=True)

        def publish(self):
            self.updated_date = timezone.now()
            self.save()

        def __str__(self):
            return self.title

【问题讨论】:

    标签: python django docker docker-compose dockerfile


    【解决方案1】:

    如果您收到 No changes detected 表明 Django 没有发现您的 models.py,因为您也将该项目用作您的应用程序,我猜您忘记添加您的项目名称(这是相同的作为您的应用名称)在您的INSTALLED_APPS 中。

    例如在settings.py

    INSTALLED_APPS = (
        ...
        "trialriskincApi",
    )
    

    【讨论】:

    • 我已将 models.py 文件添加到与我的 settings.py 文件相同的文件夹中,你能检查我的文件夹结构吗,我已在我的问题中添加了该文件
    • 你的INSTALLED_APPS里面有什么?
    • 仍然没有得到那个模型,你能看到我的文件夹结构并告诉我我的模型是否在正确的文件夹中
    • 你也可以发布你的settings.py 吗?
    • 我已经在github中添加了,确保项目在docker github.com/nikultaka/python-issue
    【解决方案2】:

    要迁移新模型,您必须运行 python manage.py makemigrations <yourAppName>

    对于 Docker,您可以这样做: docker exec -ti 75ce87c91dc7 sh -c "python app/manage.py makemigrations <yourAppName>

    【讨论】:

    • 但我正在使用 docker
    • 是的!所以你喜欢这个docker exec -ti 75ce87c91dc7 sh -c "python app/manage.py makemigrations <yourAppName>
    • 它说没有检测到变化
    • 我们是否需要在其他地方定义该模型?
    • 你在models/__init__.py中导入了吗?
    猜你喜欢
    • 2018-11-25
    • 2020-02-12
    • 1970-01-01
    • 2015-06-29
    • 2021-08-29
    • 1970-01-01
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多