i am trying to get data from django server and get this error.
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'
data/views:
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/serializers:
from rest_framework import serializers
from .models import SiteInfo
class SiteInfoSerializer(serializers.ModelSerializer):
model = SiteInfo
fields = ('site_id', 'site_name', 'latitude', 'longitude')
what could be the problem? thanks
Your serializer needs to use a nested Meta
class to declare model
and fields
(you were declaring them as normal attributes of the class instead of nesting them):
class SiteInfoSerializer(serializers.ModelSerializer):
class Meta:
model = SiteInfo
fields = ('site_id', 'site_name', 'latitude', 'longitude')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With