gpt4 book ai didi

python - Queryset.create() 方法中的 Django, 'self._for_write' 是什么?

转载 作者:行者123 更新时间:2023-12-04 11:37:15 25 4
gpt4 key购买 nike

在下面这段代码中,代码self._for_write 是什么意思?意思?

def create(self, **kwargs):
"""
Create a new object with the given kwargs, saving it to the database
and returning the created object.
"""
obj = self.model(**kwargs)
self._for_write = True
obj.save(force_insert=True, using=self.db)
return obj

最佳答案

基于 django.db.models.query.QuerySet.db

@property
def db(self):
"""Return the database used if this query is executed now."""
if self._for_write:
return self._db or router.db_for_write(self.model, **self._hints)
return self._db or router.db_for_read(self.model, **self._hints)
这意味着当您的 QuerySetself._for_write == True ,它将使用 db_for_write 触发您的查询.更多详情请阅读 Django document db_for_write

关于python - Queryset.create() 方法中的 Django, 'self._for_write' 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50263808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com