我正在尝试构建一个api来通过dropzone上传图片。
以下是我的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')
以下是我的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"
当我从浏览器访问API时出现以下错误
'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
我是DRF的新手。
请告诉我这里我做错了什么。
提前致谢。
Sukalpo。
答案 0 :(得分:3)
您有拼写错误 - 它应该是fields
,而不是field
:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'facebook_id', 'first_name', 'last_name', 'access_token')
# HERE ^
答案 1 :(得分:0)
from rest_framework import serializers
from .models import Post
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = ('title','slug','content')
仍然是元组没有显示属性