Django:ModelState的作用是什么?

Django:ModelState的作用是什么?,第1张

Django:ModelState的作用是什么?

在Django源代码中,_state是在每个Model实例定义的实例变量,该实例变量的实例

ModelState
定义为:

class ModelState(object):    """    A class for storing instance state    """    def __init__(self, db=None):        self.db = db        # If true, uniqueness validation checks will consider this a new, as-yet-unsaved object.        # Necessary for correct validation of new instances of objects with explicit (non-auto) PKs.        # This impacts validation only; it has no effect on the actual save.        self.adding = True

因此,基本上,该实例变量用于了解

Model
实例是否已写入
db
(知道Django支持多个数据库后端)并保存
db
使用的实例变量,该实例变量属性
adding
在保存模型实例后设置为false
,并且大多数情况下使用(作为上面代码中的注释),用于验证主键是否唯一。



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5648002.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存