Django Error The `fields` option must be a list or tuple or "__all__"
Asked Answered
L

2

6
TypeError at /api/team/
The `fields` option must be a list or tuple or "__all__". Got str.
Request Method: GET
Request URL:    http://127.0.0.1:8000/api/team/
Django Version: 1.9
Exception Type: TypeError
Exception Value:    
The `fields` option must be a list or tuple or "__all__". Got str.
Exception Location: /Library/Python/2.7/site-packages/rest_framework/serializers.py in get_field_names, line 971
Python Executable:  /usr/bin/python
Python Version: 2.7.10
Python Path:    
['/Desktop/webprog/python/wsgi/openshift',
 '/Library/Python/2.7/site-packages/Django-1.9-py2.7.egg',
 '/Library/Python/2.7/site-packages',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC']
Server time:    Mon, 28 Mar 2016 14:42:11 +0000

What does this error mean and where does it occur? It says Exception Location but that is within the rest_framework...but the error is obviously within my code, how can I find out? :S

serializers.py

from api.models import Team
from rest_framework import serializers

class TeamSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Team
        fields = ('name')
Lentissimo answered 28/3, 2016 at 14:47 Comment(1)
Can you add your other code parts(serializer)Otolith
F
27

You just need to change this:

fields = ('name')

to this:

fields = ('name',)

Note the comma. A tuple with only one object requires a trailing comma to distinguish it from a parenthesized object.

Frontolysis answered 28/3, 2016 at 16:29 Comment(0)
Y
0

Only one value without a comma at the end in parentheses "()" is not recognized as Tuple.

So, this code is:

fields = ('name') # Is not Tuple

Same as this code:

fields = 'name' # Is not Tuple

So, a comma is needed at the end to be recognized as Tuple:

fields = ('name',) # Is Tuple

In addition, multiple values with and without a comma at the end in parentheses "()" are both recognized as Tuple:

fields = ('name', 'age', 'gender') # Is Tuple

fields = ('name', 'age', 'gender',) # Is Tuple

In addition again, no value without a comma in parentheses "()" is recognized as Tuple:

fields = () # Is Tuple

But no value with a comma in parentheses "()" gets error:

fields = (,) # Error
Ytterbium answered 15/3, 2022 at 17:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.