首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型转换queryset to list() error in view function -适用于shell

类型转换queryset to list() error in view function -适用于shell
EN

Stack Overflow用户
提问于 2011-07-19 10:42:27
回答 1查看 168关注 0票数 1

sI具有以下代码:

代码语言:javascript
复制
s = StoryCat.objects.filter(category=c)
ids=s.values_list('id',flat=True)
ids=list(ids)
str= json.dumps( ids )
return HttpResponse(str)

当尝试使用python shell时,它运行得很好。当在视图函数中运行它时,我得到以下错误:

list()恰好接受2个参数(给定1个)

可能的问题是什么?

EN

回答 1

Stack Overflow用户

发布于 2011-07-19 11:01:07

已在本地作用域中重写列表内置。如果您真的想使用list(),下面是一个解决方法的示例:

代码语言:javascript
复制
def list(a, b): pass # somewhere list is redefined
try:
    c = list() # so this will fail
except TypeError as e:
    print "TypeError:", e # with this error
from __builtin__ import list as lst # but we can get back the list builtin
c = lst() # and use it without overriding the local version of list
print c

在您的示例中,最小的更改是将ids=list(ids)替换为

代码语言:javascript
复制
ids = __import__('__builtin__').list(ids)

这根本不会改变您的命名空间,但会让我感到悲伤。

编辑:查看@Alex-Laskin的评论,了解更简单的一次性方法。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6741748

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档