How to resolve this error code below, 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)

This is the full error message.

RelatedObjectDoesNotExist at /api/following/ User has no following. Request Method: POST Request URL: http://127.0.0.1:8000/api/following/ Django Version: 3.2.7 Exception Type: RelatedObjectDoesNotExist Exception Value:
User has no following. Exception Location: C:\Users\user\Desktop\myhallel-project\venv\lib\site-packages\django\db\models\fields\related_descriptors.py, line 421, in get Python Executable: C:\Users\user\Desktop\myhallel-project\venv\Scripts\python.exe Python Version: 3.9.6 Python Path:
['C:\Users\user\Desktop\myhallel-project\myhallel', 'C:\Users\user\AppData\Local\Programs\Python\Python39\python39.zip', 'C:\Users\user\AppData\Local\Programs\Python\Python39\DLLs', 'C:\Users\user\AppData\Local\Programs\Python\Python39\lib', 'C:\Users\user\AppData\Local\Programs\Python\Python39', 'C:\Users\user\Desktop\myhallel-project\venv', 'C:\Users\user\Desktop\myhallel-project\venv\lib\site-packages']

Back to Top