Methods for adding JSON type fields to model models in Django

Source: Internet
Author: User
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.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.