Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django/ python validate JSON

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

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

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

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.

like image 361
9-bits Avatar asked Dec 17 '22 04:12

9-bits


1 Answers

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()
like image 86
jdi Avatar answered Dec 18 '22 18:12

jdi