FieldError at / Related Field got invalid lookup: is_null

Viewed 509

I'm creating a comment api but when i run the server give this error:

FieldError at / Related Field got invalid lookup: is_null

i don't know how to fix it. i'm creating a nested comment api. this is my code:

#serializer

class CommentSerializer(serializers.ModelSerializer):
    loadParent = serializers.SerializerMethodField("loadPrentData")

    def loadPrentData(self, comment):
        comments = Comment.objects.filter(parent=comment)
        comments_ser = CommentSerializer(comments, many=True).data
        return comments_ser

    class Meta:
        model = Comment
        fields = ['id', 'user', 'product', 'parent', 'body', 'created', 'loadParent']


class ProductSerializer(serializers.ModelSerializer):
    comments = serializers.SerializerMethodField("loadProductComments")

    def loadProductComments(self, _product):
        _comments = Comment.objects.filter(product=_product, parent__is_null=True)
        _comments_ser = CommentSerializer(_comments, many=True, read_only=True).data
        return _comments_ser

    class Meta:
        model = Product
        fields = ['id', 'category', 'name', 'slug', 'image_1',
                  'image_2', 'image_3', 'image_4', 'image_5',
                  'description', 'price', 'available', 'created', 'updated', 'comments']
        lookup_field = 'slug'
        extra_kwargs = {
            'url': {'lookup_field': 'slug'}
        }

#views:

@api_view()
def AddComent(request, parent_id=None):
    parent = request.data.get("parent_id")
    serializer = CommentSerializer(data=request.data)
    if serializer.is_valid():
        if parent is not None:
            comment = Comment.objects.create(user=request.user, product=serializer.validated_data['product'],
                                             parent_id=serializer.validated_data['parent'],
                                             body=serializer.validated_data['body'])
        else:
            comment = Comment.objects.create(user=request.user, product=serializer.validated_data['product'],
                                             body=serializer.validated_data['body'])

        comments_ser = CommentSerializer(comment,many=False, read_only=True).data
        return Response(comments_ser, status=status.HTTP_200_OK)
    return Response(status=status.HTTP_400_BAD_REQUEST)
1 Answers

Your line

_comments = Comment.objects.filter(product=_product, parent__is_null=True)

should be

_comments = Comment.objects.filter(product=_product, parent__isnull=True)
Related