django postgresql json field schema validation

后端 未结 4 1375
半阙折子戏
半阙折子戏 2021-02-05 09:59

I have a django model with a JSONField (django.contrib.postgres.fields.JSONField) Is there any way that I can validate model data against a json schema file?

4条回答
  •  無奈伤痛
    2021-02-05 10:18

    That's what the Model.clean() method is for (see docs). Example:

    class MyData(models.Model):
        some_json = JSONField()
        ...
    
        def clean(self):
            if not is_my_schema(self.some_json):
                raise ValidationError('Invalid schema.')
    

提交回复
热议问题