Django view, interacting with the database and returning data

Source: Internet
Author: User
Tags sqlite database

Environment: Python 2.7.13 Database: Sqlite3 (Django comes with)

When I was learning about Django, I was having trouble. It is probably not the data that has been transferred to JSON data since it was taken to the database. Finally I finally figured out the solution.

The point is that when interacting with the SQLite database through the Django API, it is important to see what type of object is returned.

First, with the database to do the interaction, simply speaking, is nothing more than adding and deleting. First of all, say "check", will be updated in the future

Check

Several more commonly used methods have been checked on the Internet.

models. UserInfo.objects.all ()
models. UserInfo.objects.all (). VALUES (' user ') #只取user列
Models. UserInfo.objects.all (). Values_list (' id ', ' user ') #取出id和user列 and generate a list
models. UserInfo.objects.get (id=1)Models. UserInfo.objects.get (user= ' YANGMV ') when using models. Interface.objects.all () to query the time, the author of the source code is as follows
def Returndata (Request):     = models. Interface.objects.all ()    = serializers.serialize ('json', info)     return HttpResponse (isdict, content_type="application/json")

At this point you can see the type of info as queryset by breaking point

At this point we use Serializers.serialize (' json ', info) to parse the conversion, and finally return through HttpResponse normal

When using models. Interface.objects.get (payload=123456) to query the time, the author of the source code is as follows

def Returndata (Request):     = Models. Interface.objects.get (payload=123456)    = {}    = model_to_dict (info)    Response_data ["resultcode"] = 0    response_data["message  "] = isdict    return HttpResponse (Jsonresponse (response_data), Content_ Type="application/json")

At this point you can see the type of info as interface by breaking point

At this point we use Model_to_dict (info), to resolve the conversion, and finally through HttpResponse normal return

Django view, interacting with the database and returning data

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.