Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:参数类型为';功能';不可编辑:django错误_Python_Django_Function_Typeerror - Fatal编程技术网

Python TypeError:参数类型为';功能';不可编辑:django错误

Python TypeError:参数类型为';功能';不可编辑:django错误,python,django,function,typeerror,Python,Django,Function,Typeerror,我正在为民意测验应用程序创建API,我陷入了这个错误: error.TypeError:类型为“function”的参数不可iterable:django 错误:我的模型: 我该怎么处理呢?代码中似乎没有错误 代码: from django.db import models from django.contrib.auth.models import User class Poll(models.Model): question = models.CharField(max_leng

我正在为民意测验应用程序创建API,我陷入了这个错误

error.TypeError:类型为“function”的参数不可iterable:django 错误:我的模型:

我该怎么处理呢?代码中似乎没有错误

代码:

from django.db import models
from django.contrib.auth.models import User


class Poll(models.Model):
    question = models.CharField(max_length=100)
    created_by = models.ForeignKey(User, on_delete=models.CASCADE)
    pub_date = models.DateTimeField(auto_now=True)

def __str__(self):
    return self.question


class Choice(models.Model):
    poll = models.ForeignKey(Poll, related_name='choices', on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=100)

def __str__(self):
    return self.choice_text


class Vote(models.Model):
    choice = models.ForeignKey(Choice, related_name='votes', on_delete=models.CASCADE)
    poll = models.ForeignKey(Poll, on_delete=models.CASCADE)
    voted_by = models.ForeignKey(User, on_delete=models.CASCADE)

class Meta:
    unique_together = ("poll", "voted_by")
我的观点:

from django.shortcuts import render, get_object_or_404
    from django.http import JsonResponse

from .models import Poll

def polls_list(request):
    MAX_OBJECTS = 20
    polls = Poll.objects.all()[:MAX_OBJECTS]
    data = {"results": list(polls.values("question", "created_by__username", "pub_date"))}
    return JsonResponse(data)


def polls_detail(request, pk):
    poll = get_object_or_404(Poll, pk=pk)
    data = {"results": {
        "question": poll.question,
        "created_by": poll.created_by.username,
        "pub_date": poll.pub_date
     }}
   return JsonResponse(data)

试试这个,因为我不确定切片的效果如何:


def polls_list(request):
    MAX_OBJECTS = 20
    polls = Poll.objects.values("question", "created_by__username", "pub_date")
    data = {"results": list(polls[:MAX_OBJECTS])}
    return JsonResponse(data)

显示回溯,以便我们可以看到错误发生的位置。