Django / python验证JSON [英] Django/ python validate JSON

查看:115
本文介绍了Django / python验证JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django / python中验证JSON数据的最佳方法是什么。

what is the best way to validate JSON data in Django/python.

最好创建一堆类,例如Django FormMixin类,以验证数据/参数被传入?

Is it best to create a bunch of classes like the Django FormMixin classes that can validate the data/ parameters being passed in?

执行此操作的最佳DRY方法是什么?我可以利用现有的应用程序吗?

What's the best DRY way of doing this? Are there existing apps that I can leverage?

我想获取JSON数据并对其模型实例执行一些操作/更新。我要接收的数据不是用户生成的-即它们是ID和标志(无文本),因此我不想使用表单。

I'd like to take in JSON data and perform some actions/ updates to my model instances as a result. The data I'm taking in is not user generated - that is they are id's and flags (no text) so I don't want to use Forms.

推荐答案

我只是从json数据实例化模型对象,然后在模型上调用full_clean()进行验证:https://docs.djangoproject.com/en/dev/ref/models /instances/#django.db.models.Model.full_clean

I just instantiate a model object from the json data and call full_clean() on the model to validate: https://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.full_clean

m = myModel(**jsondata)
m.full_clean()

这篇关于Django / python验证JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆