简体   繁体   English

Django-Rest-Framework 序列化器类元

[英]Django-Rest-Framework serializer class meta

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这是 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. TypeError at /api/posts/ fields选项必须是列表或元组。 Got str.得到了 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.请求方法:GET 请求 URL: http : //127.0.0.1 :8000/api/ posts/ Django 版本:1.8.3 异常类型:TypeError 异常值: fields选项必须是列表或元组。 Got str.得到了 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异常位置:/home/root-master/restcosolg/cslg/local/lib/python2.7/site-packages/rest_framework/serializers.py in get_field_names, line 900 Python 可执行文件:/home/root-master/restcosolg/cslg/ bin/python Python 版本:2.7.6

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:因此,如果您不想使用__all__值,但模型中只有 1 个值,则需要确保在 fields 部分中有一个逗号:

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

Update (5 May 2016):更新(2016 年 5 月 5 日):

__all__ value for fields is now supported in ModelSerializer (Thanks @wim for pointing out). ModelSerializer 现在支持fields __all__值(感谢@wim指出)。

You can also set the fields attribute to the special value '__all__' to indicate that all fields in the model should be used.您还可以fields属性设置为特殊值'__all__'以指示应使用模型中的所有字段。

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 .如果您只想在模型序列化程序中使用默认字段的子集,您可以使用fieldsexclude选项来实现,就像使用ModelForm It is strongly recommended that you explicitly set all fields that should be serialized using the fields attribute.强烈建议您使用fields属性显式设置应序列化的所有字段。 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.您似乎正在尝试将 Django ModelForm fields属性与 DRF 序列化程序fields属性混合使用。
In a DRF serializer, __all__ is an invalid value for the fields attribute.在 DRF 序列化程序中, __all__fields属性的无效值。

Secondly, you can't specify multiple models in a Meta class.其次,您不能在Meta类中指定多个模型。 You will need to use 2 separate serializers and attach them with each other.您将需要使用 2 个单独的序列化程序并将它们相互连接。

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM