更新时间:2023-11-30 17:03:22
1) form.is_valid()
->form.full_clean()
-->form._clean_fields()
---> self.cleand_data[name] = field.clean(value)
2) field.clean(value)
-> self.to_python(value)
-> self.validate(value)
查看源代码时,您会发现,这主要是因为 empty_values
支票。
when look into the source code ,you can find that,it's mainly because the empty_values
check.
# These values, if given to validate(), will trigger the self.required check.
EMPTY_VALUES = (None, '', [], (), {})
如您所见,空字典 {}
是JSONField的空值。
as you can see the empty dict {}
is as an empty value for JSONField. so it will raise Error.
forms.py
from django.contrib.postgres import forms
class MyJSONField(forms.JSONField):
empty_values = [None, "", [], ()]
db /fields.py
db/fields.py
class MyJSONField(JSONField):
def formfield(self, **kwargs):
from ..forms import MyJSONField
return super().formfield(**{"form_class": MyJSONField, **kwargs})