我想從django服務器獲取數據並獲取此錯誤。AttributeError'元組'對象沒有屬性'值'django rest框架
Internal Server Error: /data/site-info/
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/rest_framework/views.py", line 466, in dispatch
response = self.handle_exception(exc)
File "/Library/Python/2.7/site-packages/rest_framework/views.py", line 463, in dispatch
response = handler(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/rest_framework/decorators.py", line 53, in handler
return func(*args, **kwargs)
File "/Users/hco/PycharmProjects/tool/linyit/data/views.py", line 17, in site_info
Response(serializer.data)
File "/Library/Python/2.7/site-packages/rest_framework/serializers.py", line 674, in data
ret = super(ListSerializer, self).data
File "/Library/Python/2.7/site-packages/rest_framework/serializers.py", line 239, in data
self._data = self.to_representation(self.instance)
File "/Library/Python/2.7/site-packages/rest_framework/serializers.py", line 614, in to_representation
self.child.to_representation(item) for item in iterable
File "/Library/Python/2.7/site-packages/rest_framework/serializers.py", line 459, in to_representation
fields = self._readable_fields
File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Library/Python/2.7/site-packages/rest_framework/serializers.py", line 353, in _readable_fields
field for field in self.fields.values()
AttributeError: 'tuple' object has no attribute 'values'
程序my_app/urls.py:
urlpatterns = [
url(r'^site-info/$', site_info, name='site_info'),
]
程序my_app/models.py:
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class SiteInfo(models.Model):
site_id = models.IntegerField(primary_key=True)
site_name = models.CharField(max_length=100, blank=True, null=True)
latitude = models.DecimalField(max_digits=18, decimal_places=15, blank=True, null=True)
longitude = models.DecimalField(max_digits=18, decimal_places=15, blank=True, null=True)
class Meta:
# managed = False
db_table = 'site_info'
數據/視圖:
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .models import SiteInfo
from .serializers import SiteInfoSerializer
# Create your views here.
@api_view(['GET', 'POST'])
def site_info(request):
"""
List all sites
"""
if request.method == 'GET':
sites = SiteInfo.objects.using('teldata').all()
serializer = SiteInfoSerializer(sites, many=True)
Response(serializer.data)
程序my_app /串行化器:
from rest_framework import serializers
from .models import SiteInfo
class SiteInfoSerializer(serializers.ModelSerializer):
model = SiteInfo
fields = ('site_id', 'site_name', 'latitude', 'longitude')
可能是什麼問題?感謝
你'return'作爲前綴'響應(serializer.data)'在你的視圖上,對吧? – trinchet
是的,你是對的,我不知道我怎麼看不到它。對不起,這愚蠢misameke :( – hco