在DRF(django-rest-framework)中,AttributeError'str'对象没有属性'~~'如何解决?

时间:2016-07-24 18:37:36

标签: python django django-rest-framework

我正在使用DRF并且在AttributeError的前面'str'对象没有属性'~~'。

我的错误页面和代码

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/blog/

Django Version: 1.9.7
Python Version: 3.5.2
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.gis',
 'blog',
 'account',
 'taggit',
 'friendship',
 'rest_framework']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)

File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/rest_framework/views.py" in dispatch
  466.             response = self.handle_exception(exc)

File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/rest_framework/views.py" in dispatch
  463.             response = handler(request, *args, **kwargs)

File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/rest_framework/decorators.py" in handler
  52.             return func(*args, **kwargs)

File "/home/keepair/djangogirls/blog/views.py" in post_list
  37.         return Response(serializer.data)

File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/rest_framework/serializers.py" in data
  700.         ret = super(ListSerializer, self).data

File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/rest_framework/serializers.py" in data
  239.                 self._data = self.to_representation(self.instance)

File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/rest_framework/serializers.py" in to_representation
  618.             self.child.to_representation(item) for item in iterable

File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/rest_framework/serializers.py" in <listcomp>
  618.             self.child.to_representation(item) for item in iterable

File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/rest_framework/serializers.py" in to_representation
  463.                 attribute = field.get_attribute(instance)

File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/rest_framework/relations.py" in get_attribute
  157.         return get_attribute(instance, self.source_attrs)

File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/rest_framework/fields.py" in get_attribute
  83.                 instance = getattr(instance, attr)

Exception Type: AttributeError at /blog/
Exception Value: 'str' object has no attribute 'author'

我想知道如何制作serializers.py代码。

我已经研究过:http://www.django-rest-framework.org/api-guide/relations/

但我不明白我该做什么。我应该把serializers.py放在哪里? 以及如何使我的序列化代码?或者使用DRF时models.ForienKey可能无法使用?

博客/ views.py

@api_view(['GET'])
def post_list(request, format=None):
    """
    List all snippets, or create a new snippet.
    """
    if request.method == 'GET':
        lat = request.POST.get('user_lat', '13')
        lon = request.POST.get('user_lon', '15')
        userpoint = GEOSGeometry('POINT(' + lat + ' ' + lon + ')', srid=4326)
        i=1

        while i:
            list_i = Post.objects.filter(point__distance_lte = (userpoint, D(km=i)))
            list_total = str(',' + ' list_i')
            post_list = list(chain(list_total))
            if len(post_list) >= 0 :
                break

        serializer = PostSerializer(post_list, many=True)
        return Response(serializer.data)

1 个答案:

答案 0 :(得分:1)

这与您的序列化程序或放置它的位置无关。错误回溯告诉您错误发生在视图中。

因此,在post_list视图中,您构建了一个列表(也称为post_list),该列表由字符串列表填充。然后你尝试通过PostSerializer,它当然期待Posts的查询集。

我不确定清单的重点是什么;好像你应该将帖子直接传递给序列化器。