📜  使用可调用对象,例如,使用 `dict` 而不是 `{}` - Python 代码示例

📅  最后修改于: 2022-03-11 14:47:12.127000             🧑  作者: Mango

代码示例1
# You have two options here:

# 1.Rely on dict as a default; this will result in your models using an empty dict {} if none is supplied:
class UnderwritingValidator(TimeStampedModel):
    plan = models.PositiveIntegerField(null=True, blank=True, unique=True)
    logic = JSONField(default=dict)

# 2.Create your own "callable" and use it as default:
def get_default_something():
    return {'accept_list': [], 'reject_list': []}

class UnderwritingValidator(TimeStampedModel):
    plan = models.PositiveIntegerField(null=True, blank=True, unique=True)
    logic = JSONField(default=get_default_something)