Django docker db迁移不适用于新mod

2024-09-26 22:51:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新来的djangodocker。你知道吗

我已经为django应用程序创建了新模型,我正在docker上工作。当我尝试使用migrate命令docker exec -ti 75ce87c91dc7 sh -c "python app/manage.py migrate"时,它说:No migrations to apply。在这里我添加了我的型号.py文件,是否需要将此模型导入其他任何位置?你知道吗

文件夹结构:

enter image description here

你知道吗设置.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',
]

你知道吗型号.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

Tags: djangohttpsimportcomauthtruedocssettings
2条回答

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

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

如果你得到的No changes detected表明Django没有发现你的models.py,考虑到你也在使用这个项目作为你的应用程序,我猜你忘了在INSTALLED_APPS中添加你的项目名(与你的应用程序名相同)。你知道吗

例如在settings.py

INSTALLED_APPS = (
    ...
    "trialriskincApi",
)

相关问题 更多 >

    热门问题