When querying Django via REST using the Django REST framework I get the error,
使用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
My settings are....
我的设置是......
settings.py
settings.py
THIRD_PARTY_APPS = (
'south', # Database migration helpers:
'crispy_forms', # Form layouts
'rest_framework',
)
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.AllowAny',
)
}
views
意见
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
urls.py
urls.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')),
)
Serializers
串行器
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',)
Models
楷模
from django.db import models
class Abc(models.Model):
test = models.CharField(max_length=12)
Any ideas ?
有任何想法吗 ?
2 个解决方案
#1
14
You need to use a tuple or list for fields
, to represent a tuple with single item you need to use a trailing comma:
您需要为字段使用元组或列表,以表示需要使用尾随逗号的单个项目的元组:
fields = ('test', )
Without a comma fields = ('test')
is actually equivalent to fields = 'test'
.
没有逗号字段=('test')实际上等于fields ='test'。
From docs:
来自docs:
A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these. Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses). Ugly, but effective.
一个特殊的问题是构造包含0或1项的元组:语法有一些额外的怪癖来适应这些。空元组是由一对空括号构成的;通过使用逗号跟随值来构造具有一个项目的元组(在括号中包含单个值是不够的)。丑陋但有效。
#2
5
('test')
is not a tuple, it is the same value as simply 'test'
.
('test')不是一个元组,它与'test'的值相同。
You should add a trailing comma to create a singleton tuple:
您应该添加一个尾随逗号来创建单例元组:
fields = ('test',)
Or you can use a list and don't bother about the commas:
或者您可以使用列表而不必担心逗号:
fields = ['test']
#1
14
You need to use a tuple or list for fields
, to represent a tuple with single item you need to use a trailing comma:
您需要为字段使用元组或列表,以表示需要使用尾随逗号的单个项目的元组:
fields = ('test', )
Without a comma fields = ('test')
is actually equivalent to fields = 'test'
.
没有逗号字段=('test')实际上等于fields ='test'。
From docs:
来自docs:
A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these. Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses). Ugly, but effective.
一个特殊的问题是构造包含0或1项的元组:语法有一些额外的怪癖来适应这些。空元组是由一对空括号构成的;通过使用逗号跟随值来构造具有一个项目的元组(在括号中包含单个值是不够的)。丑陋但有效。
#2
5
('test')
is not a tuple, it is the same value as simply 'test'
.
('test')不是一个元组,它与'test'的值相同。
You should add a trailing comma to create a singleton tuple:
您应该添加一个尾随逗号来创建单例元组:
fields = ('test',)
Or you can use a list and don't bother about the commas:
或者您可以使用列表而不必担心逗号:
fields = ['test']