Click here to Skip to main content
15,393,377 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Validate the user if the review for the book already exists or not in DRF


I'm using the Django rest framework to implement a books reviews app where I need to validate the user for 2nd review for that book using ModelViewSet .

What I have tried:

Here is my
ReviewsSerializer


class ReviewsSerializer(serializers.ModelSerializer):
      
        
        class Meta:
            fields = ('id', 'user', 'text', 'created_at','book','rating')
            model = Reviews

        def validate_user(self, user):


            data = Reviews.objects.filter(user=user)
            print(data)
            request = self.context['request']
            c =  request.data

            # if Reviews.objects.filter(book=request.user).exists():
            #     print("ex")
            # else:
            #  print("no")

            #g = Reviews.objects.get(book=request.user)

            #book= get_object_or_404(book, pk=request.c)

            book_id = self.instance
               #book_id = self.context['request'].get('book_id')
            book_id = self.instance.id

            ca = Reviews.objects.filter(company=book_id, user=request.user).exists()

            try:
               pass
               #g = Reviews.objects.filter(book__user__reviews=request)
               #g = Reviews.objects.filter(book=request.user).exists()
               # return user
            except:
                raise serializers.ValidationError(("user already submitted a review for that book"))

            return user
Posted
Comments
OriginalGriff 11-Dec-21 1:43am
   
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900