django模型类单例/只实现一次吗?这样
from .models import MODEL
# some code
a = MODEL
# in some other file
from .models import MODEL
# some code
b = MODEL
# some code
a is b # always True?
答案 0 :(得分:1)
模型类本身不是单例,可以多次实例化,但Python的导入机制只会在每个导入路径中实例化一次;后续导入的同一模块将复制对现有导入的引用。
TL; DR:除非出现奇怪的导入边缘情况,否则您的代码应该有效。