Ask how we can resolve the User matching query does not exist error in the long storage.

Asked 2 years ago, Updated 2 years ago, 46 views

I am using the django rest framework in Jango, and I wanted to apply the Python package called django-freindship. And then I found a package like https://pypi.python.org/pypi/fh-drf-friendship/0.1.1 on Google, so I wanted to use it. But I'm a beginner in Python and programming, so I didn't know how to apply it even if I had this, so I tried to copy and paste the code of fh-drf-friendship myself,

In the original fh-drf-friendship code, serializers.py

User = get_user_model()
UserListSerializer = module_loading.import_string(settings.DRF_FRIENDSHIP['USER_LIST_SERIALIZER'])

There was a code like this.

I've already written something called Userseializer while writing the django rest framework, so I erased that code and copied and pasted it. And that cord didn't even seem to work when I just copied and pasted it.

views.py

class FriendViewSet(NestedViewSetMixin, mixins.ListModelMixin, mixins.CreateModelMixin,
                    mixins.DestroyModelMixin, viewsets.GenericViewSet):
    serializer_class = UserSerializer

    def create(self, request, *args, **kwargs):
        request.data['to_user'] = self.get_parents_query_dict()['user']
        serializer = FriendSerializer(data=request.data)
        if serializer.is_valid():
            instance = serializer.save()
            headers = self.get_success_headers(serializer.data)
            serializer = FriendReadSerializer(instance)
            return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def get_object(self):
        to_user = User.objects.get(pk=self.get_parents_query_dict().get('user'))
        from_user = User.objects.get(pk=self.kwargs.get('pk'))
        return Friend.objects.get(to_user=to_user, from_user=from_user)

serializers.py

  class UserSerializer(serializers.ModelSerializer):
        related_postwriter = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
        related_commentwriter = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
        class Meta:
            model = User
            fields = ('id', 'username', 'email', 'related_postwriter', 'related_commentwriter')


  class FriendSerializer(rest_framework_common.serializers.ModelSerializer):
        user = serializers.PrimaryKeyRelatedField(source='from_user', queryset=User.objects.all())

       class Meta:
            model = Friend
            exclude = ('url', 'from_user',)

        def create(self, validated_data):
            fr = FriendshipRequest.objects.get(from_user=validated_data['from_user'], to_user=validated_data['to_user'])

            if fr and fr.accept():
                return Friend.objects.get(from_user=validated_data['from_user'], to_user=validated_data['to_user'])


  class FriendReadSerializer(rest_framework_common.serializers.ModelSerializer):
        user = UserSerializer(source='from_user')
        created_at = serializers.DateTimeField(source='created')

        class Meta:
            model = Friend
            exclude = ('url', 'from_user', 'to_user', 'created',)

So I changed the UserListSerializer to UserSerializer and pasted it.

Was this a big problem? An error occurs.

django version == 1.9.7

django rest framework version == 3.4.1

python3 == 3.5

I'm writing it like this.

urls.py contains router.register(r'friend', views.FriendViewSet, base_name='friend') The code has been added.

Migrate and runserver processes also work normally, but if you connect to http://127.0.0.1:8000/friend/

DoesNotExist at /friend/ User matching query does not exist occurs.

Did I do something wrong?

Is it possible to solve it by knowing the model code of friendship? I don't want to post it because it's too long.

Thank you for reading.

django python

2022-09-21 14:46

1 Answers

matching query does not exist This usually occurs when objects.get does not find the information.

User.objects.get(blah)

Instead

from django.shortcuts import get_object_or_404

# ...
User.objects.get_object_or_404 (blah)

Use get_object_or_404 to prepare for the case where the data cannot be found.


2022-09-21 14:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.