Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError 'tuple' object has no attribute 'values' django rest framework

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

like image 741
hco Avatar asked Apr 18 '16 21:04

hco


1 Answers

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')
like image 79
Timmy O'Mahony Avatar answered Oct 09 '22 00:10

Timmy O'Mahony