Django断言

2024-10-02 00:27:48 发布

您现在位置:Python中文网/ 问答频道 /正文

    AssertionError at /api/purchases/person/

    It is redundant to specify `source='name'` on field 'Field' in
 serializer 'PurchaseSerializer', because it is the same as the field name. 
Remove the `source` keyword argument.

我假设它引用了这段代码,因为我没有在其他地方使用“source”:

^{pr2}$

我试图通过“person”过滤购买,这样我只能看到他们的购买,但是由于某种原因,当我输入数据库中存在的person时,就会出现上面这样的断言错误。如果我输入一个数据库中不存在的person,它不会抛出错误,但返回一个空的JSON。这应该是相反的,但不知道为什么这不起作用。在

在模型.py在

class Purchase(models.Model):
        name = models.CharField(max_length=255)

在网址.py在

url(r'^api/purchases/(?P<username>.+)/$', views.PurchaseList.as_view()),

在视图.py在

class PurchaseList(generics.ListAPIView):
    serializer_class = PurchaseSerializer

    def get_queryset(self):
        """
        This view should return a list of all the purchases for
        the user as determined by the username portion of the URL.
        """
        username = self.kwargs['username']
        return Purchase.objects.filter(name=username)

Tags: thenamepyapi数据库fieldsourceis

热门问题