如何通过django中的配置设置并保持模块解耦?

发布于 2025-01-26 06:39:41 字数 1480 浏览 2 评论 0原文

在一个Python Django项目中,我有一个带有类的模块(例如,SomeDatastore),该模块抽象文件存储行为并需要具有正确路径的配置设置(用于开发,prod,...)

当前,我已经这样做了:

# settings.py
RELEVANT_PATH = os.environ.get("DJANGO_RELEVANT_PATH", "/some/default")
...

# datastore.py
from django.conf import settings
class SomeDataStore:
    def list_files(self):
        p = Path(settings.RELEVANT_PATH) 
        files = p.glob("*.csv")
        return files
    ...

# views.py
from datastatus import SomeDataStore
def show_files(request):
    files = SomeDataStore().get_files()
    ...

目标:我想完全从Django中完全将dataStore.py模块解除,以便可以轻松地使用它。这意味着要摆脱django.conf.settingsdatastore.py.py中的用法

一种明显的方法是将路径作为初始参数作为init参数数据存储类。

# datastore.py
class SomeDataStore:
    def __init__(self, path) -> None:
        self.path=path
    ...

但是,我在许多Django视图中使用此数据存储,这需要我在实例化类时始终指定路径,例如,

# views.py
from datastatus import SomeDataStore
def show_files(request):
    files = SomeDataStore(settings.RELEVANT_PATH).get_files()
    ...

def show_something_else(request):
    somethings = SomeDataStore(settings.RELEVANT_PATH).get_something_else()
    ...

我想避免每个实例化中的需求以始终为路径指定配置设置。

问题:在Django中是否有一些干净的方法,模式或方法可以处理此问题?还是我在这里忽略了一些明显的东西?

我想到的是在settings.py中实例化datastore,但是在settings.py中创建对象似乎在膨胀。还是不是?

In a Python Django project, I have a module with a class (let's say SomeDataStore) that abstracts file storage behaviour and requires a config setting with the correct path (different for development, prod, ...)

Currently, I've done it like this:

# settings.py
RELEVANT_PATH = os.environ.get("DJANGO_RELEVANT_PATH", "/some/default")
...

# datastore.py
from django.conf import settings
class SomeDataStore:
    def list_files(self):
        p = Path(settings.RELEVANT_PATH) 
        files = p.glob("*.csv")
        return files
    ...

# views.py
from datastatus import SomeDataStore
def show_files(request):
    files = SomeDataStore().get_files()
    ...

Goal: I'd like to decouple the datastore.py module completely from django, so that it can easily be used standalone. That would mean to get rid of the django.conf.settings usage in datastore.py

One obvious way to do it would be to provide the path as an init parameter to the DataStore class.

# datastore.py
class SomeDataStore:
    def __init__(self, path) -> None:
        self.path=path
    ...

However, I use this DataStore in many Django views, and that would require me to always specify the path when instantiating the class, e.g. like so

# views.py
from datastatus import SomeDataStore
def show_files(request):
    files = SomeDataStore(settings.RELEVANT_PATH).get_files()
    ...

def show_something_else(request):
    somethings = SomeDataStore(settings.RELEVANT_PATH).get_something_else()
    ...

I'd like to avoid the need in each instantiation to always specify the config setting for the path.

Question: Is there some clean way, pattern or approach to deal with this in Django? Or am I overlooking something obvious here?

Something I thought of is instantiating the DataStore in settings.py, but creating objects in settings.py seems bloating it. Or isn't it?

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

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

发布评论

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

评论(1

蓝咒 2025-02-02 06:39:41

您可以拥有一个my_settings.py持有路径:

# my_settings.py
import os
RELEVANT_PATH = os.environ.get("whatever", "/some/default")

并且

# datastore.py
from my_settings import RELEVANT_PATH

class SomeDataStore:
    def list_files(self):
        p = Path(RELEVANT_PATH) 
        files = p.glob("*.csv")
        return files
    ...

在其他地方需要此路径时,可以使用此路径,您可以将此路径作为settings.py.py as好吧,

# settings.py
from my_settings import RELEVANT_PATH as my_relevant_path

RELEVANT_PATH = my_relevant_path

# usage in other django app files
from django.conf import settings
# use settings.RELEVANT_PATH as usual

这将提供一些脱钩,您可以在一个位置上更改路径my_settings.py并导入django外部的路径,并将其与常规django.conf一起使用。 settings.xx语法。

You could have a my_settings.py holding the PATH:

# my_settings.py
import os
RELEVANT_PATH = os.environ.get("whatever", "/some/default")

and use like

# datastore.py
from my_settings import RELEVANT_PATH

class SomeDataStore:
    def list_files(self):
        p = Path(RELEVANT_PATH) 
        files = p.glob("*.csv")
        return files
    ...

In case you need this path whithin Django elsewhere, you could have this path as part of settings.py as well

# settings.py
from my_settings import RELEVANT_PATH as my_relevant_path

RELEVANT_PATH = my_relevant_path

# usage in other django app files
from django.conf import settings
# use settings.RELEVANT_PATH as usual

This would provide for some decoupling and you can change the path at a single place my_settings.py and import the path outside django as well as use it inside django with the usual django.conf.settings.xx syntax.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文