def validate(self, value, model_instance): # json.loadsができるかどうか確認. if not self.editable: # Skip validation for non-editable fields. return models.TextField.validate(self, value, model_instance) try: Json.encode(value, ensure_ascii=False) except: raise exceptions.ValidationError(self.error_messages['notjson'])
def to_python(self, value): """ loadsしてみて成功したら結果を返す。 失敗したらそのまま返す。 """ if isinstance(value, unicode): value = StrUtil.to_s(value) if isinstance(value, str): try: value = Json.decode(value) except: pass return value
def render(self, name, value, attrs=None): try: value = Json.encode(value, ensure_ascii=False) except: pass return super(JsonInput, self).render(name, value, attrs=attrs)
def get_db_prep_value(self, value, connection, prepared=False): if value is None: return None return Json.encode(value, ensure_ascii=False)