typcasting queryset to list() errors in view function - works on the shell
sI have the following code:
s = StoryCat.objects.filter(cat开发者_如何学运维egory=c)
ids=s.values_list('id',flat=True)
ids=list(ids)
str= json.dumps( ids )
return HttpResponse(str)
This runs fine when trying it with python shell. when running it in a view function I get the following error:
list() takes exactly 2 arguments (1 given)
what could be the problem?
The list builtin has been overridden in the local scope. Here is an example of a workaround if you really want to use list():
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
In your case, a minimal change would be to replace ids=list(ids)
with
ids = __import__('__builtin__').list(ids)
which doesn't change your namespace at all, but makes me sad.
Edit: See the comment by @Alex-Laskin for a simpler one-off way of doing this.
精彩评论