Python Django User-Admin Foreign Key

i have this code in file models.py

class WatchList(models.Model):
    title = models.CharField(max_length=50)
    storyline = models.CharField(max_length=200)
    platform = models.ForeignKey(StreamPlatform, on_delete=models.CASCADE, related_name="watchlist")
    active = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title
 
 
class Review(models.Model):
    review_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="review_user")
    watchlist = models.ForeignKey(WatchList, on_delete=models.CASCADE, related_name="watchlist")
    rating = models.PositiveIntegerField(validators=[MinValueValidator(1), MaxValueValidator(5)])
    description = models.CharField(max_length=200, null=True)
    active = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True)
    update = models.DateTimeField(auto_now=True)

    def __str__(self):
        return str(self.rating)

and Serializer in file Serializers.py

class ReviewSerializer(serializers.ModelSerializer):
    review_user = serializers.StringRelatedField(read_only=True)

    class Meta:
        model = Review
        exclude = ('watchlist',)

and file views.py like this

class ReviewCreateAV(generics.CreateAPIView):
    serializer_class = ReviewSerializer
    queryset = Review.objects.all()

    def perform_create(self, serializer):
        pk = self.kwargs.get('pk')
        watchlist = WatchList.objects.get(pk=pk)
        review_user = self.request.user
        review_query = Review.objects.filter(watchlist=watchlist, review_user=review_user)
        if review_query.exists():
            raise ValidationError("You have reviewed this movie")

        serializer.save(watchlist=watchlist, review_user=review_user)

I have a question when i using foreign-key WatchList in class Review if i don't exclude 'watchlist' in serializers.py file my program will response: 400 Bad Request 'This field is required.' when i post new request (body of request don't have field watchlist)

But this is opposite for using User-Admin ( django.contrib.auth.models ) for foreign-fey i don't need to exclude that field and my program still allow me to post new request.

Everyone can explain me why this happend? or give me some key-word to find

Thank you for reading

Back to Top