在Django模型中避免循环导入(Config类) [英] Avoiding circular imports in Django Models (Config class)

查看:100
本文介绍了在Django模型中避免循环导入(Config类)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在django中创建了一个配置模型,以便站点管理员可以随时更改某些设置,但是某些模型依赖于这些配置。我正在使用Django 2.0.2和Python 3.6.4。



我在与models.py相同的目录中创建了一个config.py文件。



让我进行paracode(对代码进行paraphase?Real Enum还有更多选择):

  #来自.config import的models.py 
*

class Configuration(models.Model):
starting_money = models.IntegerField(default = 1000)

类Person(models.Model):
资金= models.IntegarField(默认= getConfig(ConfigData.STARTING_MONEY))

#config.py
from .models import配置

类ConfigData(枚举):
STARTING_MONEY = 1
def getConfig(data):
如果不是isinstance(data,ConfigData):
引发TypeError( f {数据}不是有效的配置类型)
尝试:
config = Configuration.objects.get_or_create()
除了Configuration.MultipleObjects返回:
#清理数据库以防万一存在多种配置。
Configuration.objects.exclude(Configuration.objects.first())。delete()
返回getConfig(data)
如果数据为ConfigData.MAXIMUM_STAKE:
返回config.max_stake

如何在没有导入错误的情况下做到这一点?我已经尝试过绝对导入

解决方案

您可以推迟加载模型.py 通过在 getConfig(data)函数中的中加载它。因此,我们不再需要 models.py 在我们加载 config.py 时:

 #config.py(头部没有导入)
类ConfigData(Enum):
STARTING_MONEY = 1

def getConfig(data) :
来自.models导入配置
如果不是isinstance(data,ConfigData):
引发TypeError(f {data}不是有效的配置类型)
尝试:
config = Configuration.objects.get_or_create()
除了Configuration.MultipleObjects返回:
#在存在多个配置的情况下清除数据库。
Configuration.objects.exclude(Configuration.objects.first())。delete()
返回getConfig(data)
如果数据为ConfigData.MAXIMUM_STAKE:
返回config.max_stake

因此,我们加载 models.py config.py 中的$ c>。我们仅在实际执行 getConfig 函数时检查它是否已加载(如果不加载,则加载它),此过程随后进行。 / p>

I've created a Configuration model in django so that the site admin can change some settings on the fly, however some of the models are reliant on these configurations. I'm using Django 2.0.2 and Python 3.6.4.

I created a config.py file in the same directory as models.py.

Let me paracode (paraphase the code? Real Enum has many more options):

# models.py
from .config import *

class Configuration(models.Model):
    starting_money = models.IntegerField(default=1000)

class Person(models.Model):
    funds = models.IntegarField(default=getConfig(ConfigData.STARTING_MONEY))

# config.py
from .models import Configuration

class ConfigData(Enum):
    STARTING_MONEY = 1
def getConfig(data):
    if not isinstance(data, ConfigData):
        raise TypeError(f"{data} is not a valid configuration type")
    try:
        config = Configuration.objects.get_or_create()
    except Configuration.MultipleObjectsReturned:
        # Cleans database in case multiple configurations exist.
        Configuration.objects.exclude(Configuration.objects.first()).delete()
        return getConfig(data)
    if data is ConfigData.MAXIMUM_STAKE:
        return config.max_stake

How can I do this without an import error? I've tried absolute imports

解决方案

You can postpone loading the models.py by loading it in the getConfig(data) function, as a result we no longer need models.py at the time we load config.py:

# config.py (no import in the head)
class ConfigData(Enum):
    STARTING_MONEY = 1

def getConfig(data):
    from .models import Configuration
    if not isinstance(data, ConfigData):
        raise TypeError(f"{data} is not a valid configuration type")
    try:
        config = Configuration.objects.get_or_create()
    except Configuration.MultipleObjectsReturned:
        # Cleans database in case multiple configurations exist.
        Configuration.objects.exclude(Configuration.objects.first()).delete()
        return getConfig(data)
    if data is ConfigData.MAXIMUM_STAKE:
        return config.max_stake

We thus do not load models.py in the config.py. We only check if it is loaded (and load it if not) when we actually execute the getConfig function, which is later in the process.

这篇关于在Django模型中避免循环导入(Config类)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆