TheRapture87
TheRapture87

Reputation: 1423

Django AssertionError

    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.

I assume it's referring to this code since I haven't use 'source' anywhere else:

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

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

I'm trying to be able to filter purchases by "person" so I could only see their purchases BUT for some reason when I type a person that exists in the database it comes up with an Assertion error like above. If I type a person that does NOT exist in the database it throws no errors but returns an empty JSON. This should be vice versa but not sure why this isn't working.

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)

Upvotes: 0

Views: 3514

Answers (1)

Alasdair
Alasdair

Reputation: 308849

Try removing the source keyword argument as the error message suggests:

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

Upvotes: 1

Related Questions