django-Convert database data to JSON format (ORM and SQL two cases)

Source: Internet
Author: User

Recently I'm going to have an automated OPS platform, so I'm looking at Django's knowledge.

In the actual project development, encountered a small problem: the front and back end of the data interaction is the main use of JSON. Front-end data to the server side of the simple, slightly troublesome is the server-side transfer JSON to the front-end.

First, the data is retrieved from the database, and Django uses the ORM technology by default, but as an OPS person, I actually resent the abstraction of this API. Because although it is for developers, the table of the database is abstracted into an object, easy to develop, do not need to write SQL statements, but this is also the main culprit of late program performance bottleneck (again, when troubleshooting, encountered a 70 rows of SQL statements ...). In addition, for an OPS staff, pure SQL is the basic ability, again to learn the ORM syntax, virtually increasing the cost of learning.

Anyway, this time the theme is to convert data from the database to JSON, the following will be used in both ORM and SQL two ways to share.

1. Using the Django model layer, the ORM technology is converted into JSON format

1 defGetData (Request):2    #using ORM3    #All () returns the Queryset data type; values () returns the Valuesqueryset data type4data = models. VM.objects.values ('ID','IP','Host','username')5data = Serializers.serialize ("JSON", Tomcats)6    returnJsonresponse (list (data), Safe=false)

2. Completely discard the Django model layer and convert pure SQL to JSON format

1 defGetData (Request):2 #Using SQL3 With Connection.cursor () as cursor:4Cursor.execute ('Select ID, machine, tomcathome, IPAddress, description from Tomcatdata')5data =dictfetchall (cursor)6     returnJsonresponse (data, Safe=false, json_dumps_params={'Ensure_ascii': False})

Jsonresponse object:

Class JsonResponse (data, Encoder=djangojsonencoder, Safe=true, Json_dumps_params=none,**kwargs)

This class is a subclass of Httprespon, and it differs mainly from the parent class in that:

1. Its default content-type is set to: Application/json

2. The first parameter, data should be a dictionary type, when safe this parameter is set to: False, that data can be filled in any object can be converted to JSON format, such as list, tuple, set. The default safe parameter is True. If you pass in a data datatype that is not a dictionary type, it throws an TypeError exception.

The 3.json_dumps_params parameter is a dictionary that calls the Json.dumps () method and passes the parameters in the dictionary to the method.

With the above explanations, the parameters passed in the Jsonresponse () of the above two methods are clear and unambiguous.

The data is then passed to the front end in JSON format, and the front end can be used Ajax to get it, handle it, or show it.



django-Convert database data to JSON format (ORM and SQL two cases)

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.