Django South基于环境加载fixture(开发、集成、生产)

发布于 2025-01-02 02:13:57 字数 259 浏览 6 评论 0 原文

我正在开发一个使用 Django 和 South 进行迁移的项目。我想设置一些装置,用于在某些环境(开发、演示)中填充数据库,但不能在其他环境(生产)中填充数据库。例如,我希望系统中有一些数据,以便 UI 开发人员可以在他们正在处理的界面中使用一些数据,或者这样我们就可以快速为项目经理做一个演示,而无需通过手动设置管理界面。

虽然我找到了很多将自动化测试夹具与常规夹具分开的方法,但我还没有找到任何有关根据环境加载夹具的信息。这是否可能,或者人们是否有另一种方法来解决我忽略的这个问题?

I'm working on a project that is using Django and South for migrations. I would like to set up some fixtures that would be used to populate the database in some environments (development, demo) but not in others (production). For example, I would like there to be some data in the system so the UI developer has something to work with in the interface they are working on or so we can quickly do a demo for a project manager without having to manually set things up via the admin interface.

While I have found plenty of ways to separate automated testing fixtures from regular fixtures, I have not been able to find anything about loading fixtures based on environment. Is this possible, or is there another way people solve this problem I'm overlooking?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃不饱 2025-01-09 02:13:57

对于 initial_data 灯具,您无能为力。然而,我一直觉得这些无论如何都没有达到最佳效用。您很少希望在每次调用 syncdbmigrate 时一次又一次地应用相同的固定装置。

如果您使用一些不同名称的固定装置,则可以通过将以下内容添加到正向迁移中(来自 South docs

from django.core.management import call_command
call_command("loaddata", "my_fixture.json")

所以实际上,您所需要的只是某种仅在某些环境中执行此操作的方法。对于开发人员来说,最简单的途径就是简单地依赖 DEBUG。因此,前面的代码变为:

from django.conf import settings
from django.core.management import call_command
if settings.DEBUG:
    call_command("loaddata", "dev_fixture.json")

如果您需要更好的控制,您可以创建某种在每个 local_settings.py 中不同的设置(或者您用于根据环境自定义设置的任何方法)。例如:

# local_settings.py
ENV = 'staging'

# migration
from django.conf import settings
from django.core.management import call_command
if settings.ENV == 'staging':
    call_command("loaddata", "staging_fixture.json")

There's not much you can do about initial_data fixtures. However, I've always felt those has less than optimal utility anyways. Rarely do you want the same fixture applied again and again with every call to syncdb or migrate.

If you're using some differently named fixture, you can easily cause it to run with your migration by adding the following to your forwards migration (from the South docs)

from django.core.management import call_command
call_command("loaddata", "my_fixture.json")

So really, all you need is some way to only do that in certain environments. For dev, the easiest path would be to simply rely on DEBUG. So, the previous code becomes:

from django.conf import settings
from django.core.management import call_command
if settings.DEBUG:
    call_command("loaddata", "dev_fixture.json")

If you need greater control, you can create some sort of setting that will be different in each local_settings.py (or whatever methodology you use to customize settings based on environment). For example:

# local_settings.py
ENV = 'staging'

# migration
from django.conf import settings
from django.core.management import call_command
if settings.ENV == 'staging':
    call_command("loaddata", "staging_fixture.json")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文