Django-convert database data into JSON format (ORM and SQL), jsonorm

Source: Internet
Author: User

Django-convert database data into JSON format (ORM and SQL), jsonorm

I am planning to build an automated O & M platform recently, so I am looking at Django's knowledge.

In actual project development, there is a small problem: JSON is used for data interaction between the front and back ends. It is relatively simple to transmit data from the front-end to the server. What is a little troublesome is that the server transmits JSON data to the front-end.

First, the data is obtained from the database. Django uses the ORM technology by default, but as an O & M personnel, I am actually quite disgusted with this abstract API. Although it Abstracts database tables into an object for developers, it is easy to develop and does not need to write SQL statements, but this is also the culprit of the performance bottleneck caused by later programs (when a fault is processed again, more than 70 rows of SQL statements are encountered .....), In addition, for an O & M personnel, pure SQL is the basic capability. Once again, learning the ORM syntax increases the learning cost.

In any case, the topic of this time is to convert the data obtained from the database to JSON. We will share the data in the following two ways: ORM and SQL.

1. The Django Model layer is used, and the orm technology is converted to the JSON format.

1 def getdata (request): 2 # Use ORM3 # all () to return the QuerySet data type; values () to return the ValuesQuerySet data type. 4 data = models. VM. objects. values ('id', 'IP', 'host', 'username') 5 data = serializers. serialize ("json", tomcats) 6 return JsonResponse (list (data), safe = False)

2. completely abandon the Django Model layer and convert pure SQL to JSON format

1 def getdata (request): 2 # Use SQL3 with connection. cursor () as cursor: 4 cursor.exe cute ('select id, machine, tomcathome, ipaddress, description from tomcatdata') 5 data = dictfetchall (cursor) 6 return JsonResponse (data, safe = False, json_dumps_params = {'ensure _ ascii ': False })

 

JsonResponse object:

ClassJsonResponse(Data, encoder = DjangoJSONEncoder, safe = True, json_dumps_params = None, ** kwargs)

This class is a subclass of HttpRespon. The main difference between this class and its parent class is:

1. Its default Content-Type is set to application/json.

2. the first parameter, data should be a dictionary type. When safe is set to False, data can be filled with any objects that can be converted to JSON format, such as list, tuple, set. The default safe parameter is True. If the data type you pass in is not a dictionary type, it will throw a TypeError exception.

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

 

Through the above explanation, the parameters passed in the JsonResponse () of the above two methods are clear and clear.

In this way, the data will be transmitted to the front-end in JSON format, and the front-end can be obtained through AJAX for processing or display.

 



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.