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