Ошибка ключевого слова Django, но печать kwargs показывает ключ
У меня есть этот URL, и я отправляю идентификатор текущего вошедшего пользователя (зритель (33)) и идентификатор просматриваемого пользователя (пользователь (1)) в URL:
path("users/<int:viewer>/<int:user>/profile/", OtherProfile.as_view()),
Вот представление, обрабатывающее этот URL:
class OtherProfile(generics.RetrieveAPIView):
permission_classes = [permissions.IsAuthenticated]
serializer_class = ProfileSerializer
name = "other-profile"
lookup_field = "user"
def get_queryset(self):
breakpoint()
viewer = self.kwargs["viewer"]
user = self.kwargs["user"]
return Profile.objects.all().filter(user_id=user)
Здесь находится точка останова, и результат
print(self.kwargs["viewer"]) дает 33, что правильно
print(self.kwargs["user"]) дает 1, что также правильно
Вот сериализатор профиля, указанный в serializer_class:
class ProfileSerializer(serializers.ModelSerializer):
location = serializers.SerializerMethodField()
user = UserSerializer()
followers = serializers.SerializerMethodField()
class Meta:
model = Profile
fields = [
"id",
"background",
"photo",
"first_name",
"middle_name",
"last_name",
"birthdate",
"gender",
"bio",
"occupation",
"is_verified",
"verification",
"website",
"location",
"user",
"followers",
"created_at",
"updated_at",
]
def get_location(self, obj):
location_obj = Location.objects.filter(profile=obj.id)
if location_obj:
location_obj = Location.objects.get(profile=obj.id)
location = LocationSerializer(location_obj)
return location.data
def get_followers(self, obj):
breakpoint()
followers = Follow.objects.filter(followee=obj.user.id)
return followers.count()
Я поставил точку останова на метод get_followers, чтобы я мог посмотреть на данные.
print(self.context["view"].kwargs) - печатает ["viewer", "user"]
Мой вопрос:
Почему print(self.context["view"].kwargs["user"]) выводит 33, когда в представлении kwargs user должен быть 1?
Почему при попытке print(self.context["view"].kwargs["viewer"]) выдает ошибку?