The example in this article describes the method of adding JSON-type fields to model models in Django. Share to everyone for your reference. Specific as follows:
Django has the model used in the JSON field, adding a Jsonfield automatic type as follows:
Class Jsonfield (models. TextField): __metaclass__ = models. Subfieldbase Description = "Json" def to_python (self, value): v = models. Textfield.to_python (self, value) try: return json.loads (v) [' V '] except: pass return v def Get_prep_value (self, value):
The Jsonfield type field is then defined directly for the model.
Class Category (models. Model): name = fields. Medialnamefield ()
Very convenient to use:
Copy the Code code as follows:
Category.objects.create (name= "C1", other= (1,2,3,4,5))
All types that can be serialized by JSON can be directly assigned to the other field, which is handy.
Hopefully this article will help you with Python programming.