我的模型中有一个JSONField,当我尝试在django管理页面的json中将0保存为值时,它将值保存为null。如何将零保存为值? Django版本:2.1.7
Django管理页面:
我的JSONField:
lecturer_scores = JSONField(
verbose_name=_("Lecturer Score"),
validators=[validate_lecturer_score],
default=dict,
blank=True
)
我的输入:
{
"score 1": 0,
"score 2": 5
}
保存如下:
{
"score 1": null,
"score 2": 5
}
验证者:
from django.core.validators import validate_integer
from django.core.exceptions import ValidationError
def validate_lecturer_score(value):
for k, v in value.items():
if v:
validate_integer(v)
for value in value.values():
if value:
if value < 0 or value > 100:
raise ValidationError(_("Lecturer score for user's participance rate calculation should be between 0 and 100."))
答案 0 :(得分:1)
问题出在我的信号中,我调用一个属性方法并设置值。我检查值是否为空,将其设置为null。像这样的lecturer_scores[key] = self.lecturer_scores.get(key) or None