gpt4 book ai didi

python - Django TypeError 不是 JSON 可序列化的

转载 作者:太空宇宙 更新时间:2023-11-03 11:51:04 25 4
gpt4 key购买 nike

当我尝试将数据编码为 json 时发生错误。错误是由于分页。我想要 json 编码的数据。

这是我用来从数据库中获取结果然后将其编码为 json 的代码

View .py

def pagination(obj, request):
max = request.GET.get('max') if 'max' in request.GET else 1
paginator = Paginator(obj, max) # Show 25 contacts per page
page = request.GET.get('page')
try:
return paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
return paginator.page(1)
except EmptyPage:
return ""

def folder_list(request):
folders = UserFolder.objects.filter(creator=request.user).order_by('-modified_date')
folders_dict = []
for folder in folders:
d = {}
for k, v in folder.__dict__.items():
d[k] = str(v)
d["creator__first_name"] = folder.creator.first_name
folders_dict.append(d)
folders = Common.pagination(folders_dict, request)
folders = json.dumps(folders)
return HttpResponse(folders)

非常感谢任何帮助。

谢谢

最佳答案

我已经解决了这个问题。我刚刚在像这样在 json 中编码数据时添加了“object_list”:

def folder_list(request):
folders = UserFolder.objects.filter(creator=request.user).order_by('-modified_date')
folders_dict = []
for folder in folders:
d = {}
for k, v in folder.__dict__.items():
d[k] = str(v)
d["creator__first_name"] = folder.creator.first_name
folders_dict.append(d)
folders = Common.pagination(folders_dict, request)
folders = json.dumps(folders.object_list) #added .object_list
return HttpResponse(folders)

关于python - Django TypeError <Page 1 of 8> 不是 JSON 可序列化的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27684312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com