gpt4 book ai didi

python - GAE 需要模型属性当且仅当另一个属性具有一定值吗?

转载 作者:太空宇宙 更新时间:2023-11-03 18:18:39 25 4
gpt4 key购买 nike

在 GAE 的 db.Model 属性中,我们有一个 required 参数,该参数不允许在没有该属性值的情况下创建该模型的实体。

例如:

class user(db.Model):
isFromUK = db.BoolProperty(required = True)
fromCounty = db.StringProperty()

如果 isFromUK == True,我该如何在 fromCounty 上执行本质上 required = True 的操作?

我知道这可能无法直接在 GAE 实现中实现 ( I have not found a way in docs ) - 但我想知道是否有一些简单的方法来实现这一点,也许使用 @ClassMethod

我之前没有理由使用它,所以我不确定这是否能提供解决方案。

最佳答案

这是您在继续常规(即父类(super class)'.put)之前重写.put()以进行特殊验证的方法:

class user(db.Model):
...
def put(self, *args, **kw):
if self.isFromUK:
if not self.fromCountry:
raise ValueError("Need fromCountry if isFromUK..")
super(user, self).put(*args, **kwargs)

关于python - GAE 需要模型属性当且仅当另一个属性具有一定值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24600126/

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