Sukalpo
Sukalpo

Reputation: 326

AttributeError: 'tuple' object has no attribute 'values'

I am trying to build an api to upload images through dropzone.

Following is the code for my serializer.py

from rest_framework import serializers
from models import User
from models import Photo

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        field = ('id', 'facebook_id', 'first_name', 'last_name', 'access_token')

class PhotoSerializer(serializers.HyperlinkedModelSerializer):
    owner = UserSerializer()
    class Meta:
        model = Photo
        fields = ('id', 'url', 'image', 'owner')
        readonly_fields = ('url', 'image')

Following is my models.py

from django.db import models
from django.utils.translation import ugettext_lazy as _

class User(models.Model):
    facebook_id = models.IntegerField
    first_name = models.CharField(max_length=200)
    last_name = models.CharField(max_length=200)
    email = models.CharField(max_length=150)
    class Meta:
        db_table = "users"


def upload_to(instance, filename):
    return 'user_photos/{}/{}'.format(instance.user_id, filename)


class Photo(models.Model):
    image = models.ImageField(_('image'), blank=True, null=True, upload_to='item_images')
    owner = models.ForeignKey(User,      related_name='uploaded_item_images',blank=False,)
    class Meta:
        db_table = "user_photos"

When I am hitting the API from my browser I get the following error

'tuple' object has no attribute 'values'
Request Method: GET
Request URL:    http://127.0.0.1:8000/myappapi/api/user_photos/1/
Django Version: 1.9.1
Exception Type: AttributeError
Exception Value:    
'tuple' object has no attribute 'values'
Exception Location: D:\virtualenv\myapp_api\myapp_backend\rest_framework\serializers.py in _readable_fields, line 353

I am new to DRF.

Please tell me what is it that I am doing wrong here.

Thanks in advance.

Sukalpo.

Upvotes: 4

Views: 5839

Answers (2)

sad
sad

Reputation: 1

from rest_framework import serializers
from .models import Post

class PostSerializer(serializers.ModelSerializer):
    class Meta:
        model = Post
        fields = ('title','slug','content')

still tuple has no attributes is showing

Upvotes: 0

alecxe
alecxe

Reputation: 473813

You have a typo - it should be fields, not field:

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'facebook_id', 'first_name', 'last_name', 'access_token')
      # HERE ^

Upvotes: 3

Related Questions