Converting json can only be in the form of a dictionary, and individual objects cannot be converted
Recursion: call yourself by yourself. If you don't know how many layers you have with a loop, it's hard to control.
Fibonacci:
def b(n): if n== 2 or n== 1 : return 1 return b(n- 1 )+b(n- 2 ) #n:2 b(1)+b(0)=2 num= int ( input ( 'Please enter a positive integer:' )) res=b(num) print(res)
The model is divided into two categories: classification table and commodity table.
from django.db import models from datetime import datetime # Create your models here. #分类表 class Category(models.Model): cname=models.CharField(max_length=30,null=False) parent=models.ForeignKey('Category',on_delete=None) class Meta(): db_table= 'category' #Goods table class Goods(models.Model): gname=models.CharField(max_length=50,null=False) price=models.FloatField() sum=models.IntegerField() #stock count =models.IntegerField() #stock quantity createTime=models.DateTimeField( default =datetime.now()) #stock time category=models.ForeignKey( 'Category' , on_delete = None ) class Meta(): db_table='Goods'
To convert an object to json, first convert it to a dictionary:
from django.shortcuts import render,HttpResponse import json from django.views.decorators.csrf import csrf_exempt import pymysql from .models import Category,Goods from datetime import datetime # Create your views here. @csrf_exempt#导入csrf def queryTree(request): id = request.POST.get( 'id' ) #Get the product id global categorySet #Get the global variable if id is None : #Just entered, no point, display the parent tree categorySet = Category.objects.filter( parent_id = None ) else : categorySet = Category.objects.filter( parent_id =id) #Click on the parent tree, the subset will come out list=[] #Edit json format for category in categorySet: dict={} dict['id']=category.id dict[ 'text' ]=category.cname #if len(Category.objects.filter(parent_id=dict['id']))>0:#If there is a subset below, close it and display the file dict[ 'state' ]= 'closed' list.append(dict) # jsondata=[{ # "id":1, # "text":"My Documents", # 'state':'closed'},{ # "id":2, # "text":"python5", # 'state': 'closed', # 'children':[ # { # 'id':11, # 'text':'c1' # }, # { # 'id': 22, # 'text': 'c2' # } # ] # }] return HttpResponse(json.dumps(list)) #Load tree @csrf_exempt def queryGrid(request): goodsSet=Goods.objects.all() #queryset is a query set, which is a list of each object gridJson={ 'total' :Goods.objects.count()} list=[] for goods in goodsSet:#拼接 dict={} dict['id']=goods.id dict['gname']=goods.gname dict['price']=goods.price dict['sum']=goods.sum dict['count']=goods.count dict['createTime']=datetime.strftime(goods.createTime,'%Y-%m-%d %H:%M:%S') list.append(dict) gridJson[ 'rows' ]=list #gridJson={#Find the style first, write according to the style, query the format through the small green book, look at the path, go to the folder to find, look at the format # 'total':3,'rows': [ # { # 'code':'a10','name':'beer','price':4 # }, # { # 'code': 'a11', 'name': 'wine', 'price' : 84 # }, # { # 'code': 'a12', 'name': 'cookie', 'price': 14 # }, # ] # } return HttpResponse(json.dumps(gridJson)) #returning json Format