Home > Software engineering >  Django Rest Framework Field Validation Issue
Django Rest Framework Field Validation Issue

Time:09-10

My subscription view is located inside of UserViewSet. I'm wondering why I'm getting

IntegrityError at /api/users/1/subscribe/ new row for relation "users_subscription" violates check constraint "prevent_self_subscription" DETAIL: Failing row contains (11, 1, 1).

instead of proper json answer. Somehow SubscriptionSerializer field validation doesnt wish to work. Any thoughts?

models.py

class Subscription(models.Model):
    user = models.ForeignKey(
        User, related_name='subscriber',
        on_delete=models.CASCADE)
    author = models.ForeignKey(
        User, related_name='subscribing',
        on_delete=models.CASCADE)

    class Meta:
        constraints = [
            models.UniqueConstraint(
                fields=('user', 'author'),
                name='unique_subscription'
            ),
            models.CheckConstraint(
                check=~models.Q(user=models.F('author')),
                name='prevent_self_subscription'
            )
        ]

serializers.py

class SubscriptionSerializer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(
        read_only=True, default=serializers.CurrentUserDefault())

    class Meta:
        model = models.Subscription
        fields = ('author', 'user', )
        validators = [
            serializers.UniqueTogetherValidator(
                queryset=models.Subscription.objects.all(),
                fields=['author', 'user', ]
            )
        ]

    def create(self, validated_data):
        return models.Subscription.objects.create(
            user=self.context.get('request').user, **validated_data)

    def validate_subscribing(self, value):
        if self.context.get('request').user == value:
            raise serializers.ValidationError(
                'You cant subscribe to yourself!')
        return value

views.py

    @action(['post'], detail=True)
    @permission_classes(permissions.IsAuthenticated)
    def subscribe(self, request, *args, **kwargs):
        author = get_object_or_404(models.User, id=kwargs['id'])
        data = request.data.copy()
        data.update({'author': author.id})
        serializer = serializers.SubscriptionSerializer(
            data=data, context={'request': request})
        if request.method == 'POST':
            serializer.is_valid(raise_exception=True)
            serializer.save()
            return Response(
                status=status.HTTP_201_CREATED,
                data=self.get_serializer(author).data)

CodePudding user response:

It was the wrong method name, field name is 'author' and method was validate_subscribing() instead of validate_author().

  • Related