gpt4 book ai didi

python - 对象没有属性 'build_absolute_uri' - Django Rest Framework

转载 作者:行者123 更新时间:2023-11-30 23:08:42 27 4
gpt4 key购买 nike

我试图在我的 api 中查看 URL(使用 Django Rest Framework),但出现以下错误:

'Follow' object has no attribute 'build_absolute_uri'

我不确定这意味着什么,因此我们将不胜感激。

这是回溯:

Traceback:
File "/Users/jphalis/Desktop/oby/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/jphalis/Desktop/oby/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
File "/Users/jphalis/Desktop/oby/lib/python2.7/site-packages/django/views/generic/base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "/Users/jphalis/Desktop/oby/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
451. response = self.handle_exception(exc)
File "/Users/jphalis/Desktop/oby/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
448. response = handler(request, *args, **kwargs)
File "/Users/jphalis/Desktop/oby/lib/python2.7/site-packages/rest_framework/generics.py" in get
199. return self.list(request, *args, **kwargs)
File "/Users/jphalis/Desktop/oby/lib/python2.7/site-packages/rest_framework/mixins.py" in list
44. return self.get_paginated_response(serializer.data)
File "/Users/jphalis/Desktop/oby/lib/python2.7/site-packages/rest_framework/serializers.py" in data
618. ret = super(ListSerializer, self).data
File "/Users/jphalis/Desktop/oby/lib/python2.7/site-packages/rest_framework/serializers.py" in data
213. self._data = self.to_representation(self.instance)
File "/Users/jphalis/Desktop/oby/lib/python2.7/site-packages/rest_framework/serializers.py" in to_representation
568. self.child.to_representation(item) for item in iterable
File "/Users/jphalis/Desktop/oby/lib/python2.7/site-packages/rest_framework/serializers.py" in to_representation
435. ret[field.field_name] = field.to_representation(attribute)
File "/Users/jphalis/Desktop/oby/lib/python2.7/site-packages/rest_framework/fields.py" in to_representation
1330. return method(value)
File "/Users/jphalis/Desktop/oby/src/accounts/serializers.py" in get_supporting
124. return serializer.data
File "/Users/jphalis/Desktop/oby/lib/python2.7/site-packages/rest_framework/serializers.py" in data
618. ret = super(ListSerializer, self).data
File "/Users/jphalis/Desktop/oby/lib/python2.7/site-packages/rest_framework/serializers.py" in data
213. self._data = self.to_representation(self.instance)
File "/Users/jphalis/Desktop/oby/lib/python2.7/site-packages/rest_framework/serializers.py" in to_representation
568. self.child.to_representation(item) for item in iterable
File "/Users/jphalis/Desktop/oby/lib/python2.7/site-packages/rest_framework/serializers.py" in to_representation
435. ret[field.field_name] = field.to_representation(attribute)
File "/Users/jphalis/Desktop/oby/lib/python2.7/site-packages/rest_framework/relations.py" in to_representation
264. return self.get_url(value, self.view_name, request, format)
File "/Users/jphalis/Desktop/oby/src/accounts/serializers.py" in get_url
34. request=request, format=format)
File "/Users/jphalis/Desktop/oby/lib/python2.7/site-packages/rest_framework/reverse.py" in reverse
26. return _reverse(viewname, args, kwargs, request, format, **extra)
File "/Users/jphalis/Desktop/oby/lib/python2.7/site-packages/rest_framework/reverse.py" in _reverse
39. return request.build_absolute_uri(url)

Exception Type: AttributeError at /api2/follows/
Exception Value: 'Follow' object has no attribute 'build_absolute_uri'

我假设它指的是我的模型,所以这也是:

class Follow(models.Model):
user = models.OneToOneField(MyUser)
followers = models.ManyToManyField('self',
related_name='following',
symmetrical=False)

class Meta:
app_label = 'accounts'

def __unicode__(self):
return str(self.user.username)

MyUser.profile = property(lambda u: Follow.objects.get_or_create(user=u)[0])

预先感谢您的帮助!

- - 编辑:

serializers.py:

class FollowSerializer(serializers.HyperlinkedModelSerializer):
username = serializers.CharField(source='user.username', read_only=True)
supporters = serializers.SerializerMethodField(read_only=True)
supporting = serializers.SerializerMethodField(read_only=True)

def get_supporters(self, instance):
queryset = Follow.objects.filter(following__pk=instance.pk)
serializer = UserProfileSerializer(queryset,
context={"request": instance},
many=True)
return serializer.data

def get_supporting(self, instance):
queryset = Follow.objects.filter(followers__pk=instance.pk)
serializer = UserProfileSerializer(queryset,
context={"request": instance},
many=True)
return serializer.data

class Meta:
model = Follow
fields = [
'username',
'supporters',
'supporting',
]

class UserProfileSerializer(serializers.HyperlinkedModelSerializer):
account_url = UserProfileUrlField("user_profile_detail_api")
username = serializers.CharField(source='user.username')

class Meta:
model = UserProfile
fields = [
'account_url',
'username',
]

ListAPIView:

class FollowListAPIView(generics.ListAPIView):
authentication_classes = [SessionAuthentication, BasicAuthentication, JSONWebTokenAuthentication]
permission_classes = [permissions.IsAuthenticated]
serializer_class = FollowSerializer
queryset = Follow.objects.all()

最佳答案

现在,您正在传递模型实例,而不是序列化器中的原始请求。

我怀疑您需要通过第一个序列化器调用(在 ListAPIView 中)中的上下文传递请求,然后通过序列化器类中的 self.context 访问它。 See here 。这将允许您使用请求数据来构建 uri(并避免您现在遇到的“无属性”错误,因为您将传递方法期望的对象类型)。

这涉及重写声明的方法 herehere在您的 ListAPIView 中。

关于python - 对象没有属性 'build_absolute_uri' - Django Rest Framework,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31614372/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com