Django自带的序列化工具

Django自带的序列化工具

 

普通做法

def user_list(request):
    user_list = models.User.objects.all()  # queryset
    lst = []
    for user in user_list:
        temp_dict = {}
        temp_dict['username'] = user.username
        temp_dict['password'] = user.password
        lst.append(temp_dict)
    import json
    res=json.dumps(lst,)
    return render(request, 'user_list.html', locals())
    return HttpResponse(res)

 Django自带的序列化器方式

    需要借助serializers

from django.core import serializers
def user_list(request):
    user_list = models.User.objects.all()  # queryset
    res = serializers.serialize('json', user_list)
    return HttpResponse(res)

 

END

 

posted @ 2022-03-08 14:50  Snails蜗牛  阅读(38)  评论(0编辑  收藏  举报