简体   繁体   English

Django 通过命令行设置环境变量并在迁移中使用

[英]Django set environment variable via command line and use in Migrations

Following on from this question - Specify a.env file in Django in command line or vscode launch.json .从这个问题开始 - 在命令行或 vscode launch.json 中指定 Django 中的 a.env 文件 So we have an environment file for dev, test, and production - which is specified in an environment variable ENVIRONMENT_FILE .所以我们有一个用于开发、测试和生产的环境文件——在环境变量ENVIRONMENT_FILE中指定。

The.env file holds the database connection information (connection strings). .env 文件保存数据库连接信息(连接字符串)。

However, I'm struggling to pass this information when running the migration command:但是,我在运行迁移命令时很难传递这些信息:

python manage.py migrate

I need to be able to specify which environment I want the migrations to run on.我需要能够指定我希望迁移在哪个环境上运行。

Is it possible to setup environment variables before migrations can be run?是否可以在运行迁移之前设置环境变量?

You can load in the environment variables in your settings.py file.您可以在settings.py文件中加载环境变量。

If you have separate .env files for separate environments, and it is managed by the ENVIRONMENT_FILE variable, you can use something like python-dotenv to read your env files,如果您有用于不同环境的单独.env文件,并且它由ENVIRONMENT_FILE变量管理,则可以使用python-dotenv 之类的东西来读取您的 env 文件,

import os
from dotenv import load_dotenv

load_dotenv(os.getenv("ENVIRONMENT_FILE", ".env"))

Then, if your env file looks like this然后,如果你的 env 文件看起来像这样

DATABASE_URL=some-url-over-here

you can access these anywhere in your django application using os.getenv('DATABASE_URL')您可以使用os.getenv('DATABASE_URL')在 django 应用程序中的任何位置访问这些

You can set environment variables before you run the migration command.您可以在运行迁移命令之前设置环境变量。 In PowerShell you can do something like:在 PowerShell 中,您可以执行以下操作:

$env:ENVIRONMENT_FILE='test.env'
python manage.py migrate

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Django 管理命令(python 脚本)中设置环境变量 - How to set an environment variable inside a Django management command (python script) 在 python 命令中使用环境变量 - Use environment variable in python command 如何将命令行参数从 oc start-build 传递到 dockerfile 以在 dockerfile 中设置环境变量 - How to pass command line argument from oc start-build to dockerfile to set environment variable inside dockerfile windows 中的设置命令不适用于 django SECRET_KEY | django.core.exceptions.ImproperlyConfigured:设置 SECRET_KEY 环境变量 - set command in windows not working for django SECRET_KEY | django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable 通过 python 脚本设置 shell 环境变量 - Set shell environment variable via python script 如何通过python脚本使用设置环境变量作为命令行脚本的参数 - How to use set environment variables as argument for command line scripts through python script 如何使用Django和Python设置环境变量? - How to set environment variable using Django and Python? "设置与 getenv 一起使用的环境变量(不是 GetEnvironmentVariable)" - Set environment variable for use with getenv (not GetEnvironmentVariable) 在非交互式环境中应用Django迁移 - Applying Django migrations in a non-interactive environment 如何在命令行中将环境变量传递给pytest以测试功能 - How to pass an environment variable in command line to pytest to test a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM