当使用Django REST框架通过REST查询Django时,我得到一个错误
File "/folder/pythonenv/project/lib/python2.7/site-packages/rest_framework/serializers.py", line 241, in get_fields
assert isinstance(self.opts.fields, (list, tuple)), '`fields` must be a list or tuple'
AssertionError: `fields` must be a list or tuple
我的设置是。。。。在
设置.py
^{pr2}$视图
from django.shortcuts import render
from rest_framework import viewsets
from quickstart.serializers import from quickstart.serializers import TicketInputSerializer
from models import Abc
class TicketInputViewSet(viewsets.ModelViewSet):
queryset = Abc.objects.all()
serializer_class = TicketInputSerializer
网址.py
router = routers.DefaultRouter()
router.register(r'ticket', views.TicketViewSet)
urlpatterns = patterns('',
url(r'^', include(router.urls)),
url(r'^test', include('rest_framework.urls', namespace='rest_framework')),
)
序列化程序
from models import Abc
from django.contrib.auth.models import User, Group
from rest_framework import serializers
class TicketInputSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Abc
fields = ('test',)
型号
from django.db import models
class Abc(models.Model):
test = models.CharField(max_length=12)
有什么想法吗?在
您需要为
fields
使用元组或列表,要用单个项表示元组,您需要使用尾随逗号:没有逗号
fields = ('test')
实际上相当于fields = 'test'
。在来自docs:
('test')
不是元组,它只是'test'
的{a1}值。在应添加尾随逗号以创建单例元组:
或者您可以使用列表而不必担心逗号:
^{pr2}$相关问题 更多 >
编程相关推荐