1JsonResponse HttpResponse
class ComplexEncoder(json,JsonEncoder):
def default (self ,obj):
if isintance (obj,datetime.datetime):
return obj.strftime("%y-%m-%d %H:%M%S")
elif isintance(obj,Decimal):
return str(obj)
else:
return str(obj)
HttpResponse JsonResponse
JsonResponse 比HttpResponse 多application/json
return HttpResponse(json.dumps(result,cls=ComplexEncoder,ensure_ascii=False),content_type=''application/json;charset=utf-8")
2.python 删除字段元素
pop
d={'a':1,'b':2}
d.pop('m',404) ----不报错
d.pop('a') -----返回 1
for key,value in d.items():
for key in d.keys():
for value in d.values():
3.datetime
date=datetime.datetime.now.strftime("%y-%d-%m")
time=datetime.datetime.now.strftime("%y-%d-%m %H:%M:%S")
datetime.datetime.now()+timedelta(days=-1).strftime()
days monutes hours
relativedelta
years
4.annotate aggregate
form django.db.models import Avg,Max
计算所有学生的年龄最大值 最小值 平均值 学生数量
aggregate
Students.objects.aggregate(Avg('age'))
Students.objects.aggregate(Avg('age),Min('age'),Max('age'))
同时取
网友评论