Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
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?
模型类本身不是单例的,可以多次实例化,但 Python 的导入机制只会在每个导入路径中实例化一次;同一模块的后续导入将复制对现有导入的引用。
TL;DR:您的代码应该可以工作,除非出现奇怪的导入边缘情况。