我正在尝试查看我的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()
答案 0 :(得分:2)
现在,您正在传递模型实例而不是序列化程序中的原始请求。
我怀疑你需要通过第一个串行器调用中的上下文(在ListAPIView中)传递请求,然后通过序列化器类中的self.context访问它。 See here。这将允许您使用请求数据来构建uri(并避免使用" No属性"您现在遇到的错误,因为您将传递方法所期望的那种对象)。
答案 1 :(得分:1)
通过查看回溯,您在get_url()
中覆盖的serializers.py
函数中似乎出错了。
根据DRF docs,
此方法应返回与给定对应的URL 对象
DRF reverse()函数的基本签名:
def reverse(viewname, args=None, kwargs=None, request=None, format=None, **extra):
您必须为reverse()
中定义的参数传递正确的值。由于将错误的值发送到reverse()
函数,可能会出现错误。