2016-07-24 63 views
2

我使用DRF並且在AttributeError'str'對象的前面沒有屬性'~~'。在DRF(django-rest-framework)中,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? 以及如何讓我的序列化代碼?或者可能models.ForienKey在使用DRF時不可用?

博客/ 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) 
+0

您需要在blog/views中顯示post_list函數。 –

+0

請不要在這裏發佈很長的代碼,因爲這對我們來說是一項非常繁瑣的任務。你可以考慮通過這個[鏈接](http://stackoverflow.com/help/mcve) –

+0

@DanielRoseman,添加博客/ views.py抱歉,我不知道需要什麼代碼。 – touchingtwist

回答

1

這有什麼,與你的序列化,或者你把它。錯誤回溯告訴你錯誤發生在視圖中。

因此,在您的post_list視圖中,您將建立一個列表(也稱爲post_list),該列表由字符串列表填充。然後,您嘗試將其置入PostSerializer,這當然是期待Posts的查詢集。

我不確定列表的要點是什麼;好像你應該直接將帖子傳遞給序列化程序。

+0

謝謝,但我必須使用我的自定義'post_list'。有沒有辦法讓這個post_list'序列化'? – touchingtwist

+0

你究竟想要「序列化」?你的列表完全由一系列相同的字符串組成,所有這些都是'',list_i「'。重點是什麼? –

+0

首先'',list_i'是爲了滿足itertools - chain的語法(來自:https://docs.python.org/3.3/library/itertools.html),將我的列表總結爲一個列表。例如,來自'用戶點'附近5km列表(+我的自定義選項,如作者的名字是巴士總站)post_list和post_list是從'用戶點'附近10km列表..這樣的方式,也許我想要總和5-7只列出一個列表。所以最後我想序列化最後一個列表(itertools的結果)。對不起,我可憐的英文表達。我希望這足以讓你理解我的想法。 – touchingtwist

相關問題