How to resolve error code: RelatedObjectDoesNotExist

class Following(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True) user = models.OneToOneField(settings.AUTH_USER_MODEL, related_name='following', unique=False, verbose_name=('User'), on_delete=models.CASCADE) following_user = models.ManyToManyField(settings.AUTH_USER_MODEL, verbose_name=('Following'), related_name='following_user') created_on = models.DateTimeField(default=timezone.now)

class FollowingSerializer(serializers.ModelSerializer): new_following = serializers.PrimaryKeyRelatedField(queryset=User.objects.all(),required=True,write_only=True) class Meta: model = Following fields = [ 'id', 'user', 'following_user', 'new_following', 'created_on' ] read_only_fields = ['following_user']

def create(self, validated_data):
    user = validated_data['user']
    new_following = validated_data['new_following']
    user.following.following_user.add(new_following)
    new_following.followers.following_user.add(user)
    
    return user.following

class FollowingAPIView(mixins.CreateModelMixin, mixins.DestroyModelMixin,generics.GenericAPIView):

permission_classes = [permissions.IsAuthenticated]
serializer_class = FollowingSerializer

def get_queryset(self):
    queryset = Following.objects.all()
    return queryset

def post(self, request, *args, **kwargs):
    return self.create(request, *args, **kwargs)

def delete(self, request, *args, **kwargs):
    return self.destroy(self, request, *args, **kwargs)


              
Back to Top