19
votes

as I can use two in a meta model class, when I run it I get an error How I can use the models? It is an example of Django Rest

from rest_framework import serializers
from .models import Post,Miembros

class PostSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = Post
        fields = ('id', 'url', 'titulo', 'contenido','fecha_evento','fecha_evento','banner_grande','lugar')

        model = Miembros
        fields = '__all__'

TypeError at /api/posts/ The fields option must be a list or tuple. Got str. Request Method: GET Request URL: http://127.0.0.1:8000/api/posts/ Django Version: 1.8.3 Exception Type: TypeError Exception Value: The fields option must be a list or tuple. Got str. Exception Location: /home/root-master/restcosolg/cslg/local/lib/python2.7/site-packages/rest_framework/serializers.py in get_field_names, line 900 Python Executable: /home/root-master/restcosolg/cslg/bin/python Python Version: 2.7.6

2
Did you try to add all of the field members for the Miembros as you did in the post model to see if that is the issue, or do you get the same error?ivan7707

2 Answers

17
votes

Update (5 May 2016):

__all__ value for fields is now supported in ModelSerializer(Thanks @wim for pointing out).

You can also set the fields attribute to the special value '__all__' to indicate that all fields in the model should be used.

If you only want a subset of the default fields to be used in a model serializer, you can do so using fields or exclude options, just as you would with a ModelForm. It is strongly recommended that you explicitly set all fields that should be serialized using the fields attribute. This will make it less likely to result in unintentionally exposing data when your models change.

It seems you are trying to mix Django ModelForm fields attribute with the DRF serializer fields attribute.
In a DRF serializer, __all__ is an invalid value for the fields attribute.

Secondly, you can't specify multiple models in a Meta class. You will need to use 2 separate serializers and attach them with each other.

For example you can do something like below:

from rest_framework import serializers
from .models import Post,Miembros


class MiembrosSerializer(serializers.ModelSerializer):
    """
    serializer for Miembros model
    """

    class Meta:
        model = Miembros 
        fields = '__all__' # all model fields will be included


class PostSerializer(serializers.HyperlinkedModelSerializer):
    """
    serializer for Post model
    """

    miembros = MiembrosSerializer()

    class Meta:
        model = Post
        fields = ('id', 'url', 'titulo', 'contenido','fecha_evento','fecha_evento','banner_grande','lugar')
17
votes

I know this answer is several years after the question was asked, but I've run into this situation a couple of times. For some reason it's expecting a list instead of a single value.

So if you don't want to use the __all__ value, but you only have 1 value in your model, you need to make sure there is a comma , in the fields section:

class Meta:
        model = Post
        fields = ('id',)