11

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

2
  • you have return as prefix in Response(serializer.data) on your view, right? Commented Apr 18, 2016 at 21:16
  • yeah you are right, i don't know how i can't see it. sorry for that silly misatake :( Commented Apr 19, 2016 at 21:59

2 Answers 2

45

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')
Sign up to request clarification or add additional context in comments.

3 Comments

thanks it solved my problem :) i did some other small mistakes but it was the main problem i guess
When I see these Meta classes, I tend to think of forms in Django. It's my weird way of reminding me to put it in a meta class.
same issue here... had the fields below the Meta but indentation was wrong...
1

by adding (class Meta) in serializers.py the error can get solved as per my error which i were getting.

the image which shows code of serializers.py

1 Comment

Please don't post a picture of code. Post the code itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.