Python ModelSerializer django rest 框架中的所有字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30274591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:09:26  来源:igfitidea点击:

All fields in ModelSerializer django rest framework

pythondjangodjango-rest-framework

提问by tim

models.py:

模型.py

class Car():
    producer = models.ForeignKey(Producer, blank=True, null=True,)
    color = models.CharField()
    car_model = models.CharField()
    doors = models.CharField()

serializers.py:

序列化程序.py

class CarSerializer(ModelSerializer):

    class Meta:
        model = Car
        fields = Car._meta.get_all_field_names()

So, here I want to use all fields. But I have an error:

所以,在这里我想使用所有字段。但我有一个错误:

Field name producer_idis not valid for model Car.

字段名称producer_id对模型无效Car

How to fix that?

如何解决?

Thanks!

谢谢!

采纳答案by Michael B

According to the Django REST Framework's Documentation on ModelSerializers:

根据Django REST Framework's Documentation on ModelSerializers

By default, all the model fields on the class will be mapped to a corresponding serializer fields.

默认情况下,类上的所有模型字段都将映射到相应的序列化程序字段。

This is different than Django's ModelForms, which requires you to specify the special attribute '__all__'to utilize all model fields. Therefore, all that is necessary is to declare the model.

这与Django 的 ModelForms不同,后者要求您指定特殊属性'__all__'以利用所有模型字段。因此,所需要做的就是声明模型。

class CarSerializer(ModelSerializer):
    class Meta:
        model = Car

Update (for versions >= 3.5)

更新(对于版本 >= 3.5)

The behaviour described above was deprecated in version 3.3, and forbidden since version 3.5.

上述行为在 3.3 版中已被弃用,并自 3.5 版起被禁止。

It is now mandatoryto use the special attribute '__all__'to use all fields in the Django REST Framework, same as Django Forms:

现在必须使用特殊属性'__all__'来使用 Django REST Framework 中的所有字段,与 Django Forms 相同:

Failing to set either fields or exclude raised a pending deprecation warning in version 3.3 and raised a deprecation warning in 3.4. Its usage is now mandatory.

未能设置任一字段或排除在 3.3 版中引发了未决弃用警告,并在 3.4 中引发了弃用警告。它的使用现在是强制性的。

So now it must be:

所以现在它必须是:

class CarSerializer(ModelSerializer):
    class Meta:
        model = Car
        fields = '__all__'

回答by Venu Saini

You could use fields = '__all__'to get all your fields or you could specify if you want a limited number of fields to be returned. See documentation.

您可以使用fields = '__all__'获取所有字段,也可以指定是否要返回有限数量的字段。请参阅文档

But this returns the idvalue for the foreign key field i.e. producerin your case. To get all the fields for producer, you need to create a serializer class for that too. See here.

但这会返回id外键字段的值,即producer在您的情况下。要获取 的所有字段producer,您还需要为此创建一个序列化程序类。见这里

So your updated serializers.pyshould be:

所以你更新的serializers.py应该是:

class ProducerSerializer(ModelSerializer):
    class Meta:
        model = Producer

class CarSerializer(ModelSerializer):
    producer= ProducerSerializer(read_only=True)

    class Meta:
        model = Car
        fields = ('producer', 'color', 'car_model', 'doors', )