Django AssertionError

时间:2016-03-30 17:32:55

标签: python django

    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':

class PurchaseSerializer(serializers.HyperlinkedModelSerializer):
    name = serializers.Field(source='name')

    class Meta:
        model = Purchase
        fields = ['name']

我正试图通过“人”过滤购买,所以我只能看到他们的购买但是由于某些原因当我输入数据库中存在的人时,它会出现如上所述的断言错误。如果我键入数据库中不存在的人,则不会抛出任何错误,但会返回空的JSON。这应该相反,但不确定为什么这不起作用。

Models.py

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

Urls.py

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

Views.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)

1 个答案:

答案 0 :(得分:1)

尝试删除source关键字参数,如错误消息所示:

class PurchaseSerializer(serializers.HyperlinkedModelSerializer):
    name = serializers.Field()