Python Django Rest Framework - 如何在 ModelSerializer 中添加自定义字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14583816/
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-18 11:53:29  来源:igfitidea点击:

Django Rest Framework - How to add custom field in ModelSerializer

pythondjangodjango-rest-framework

提问by Ron

I created a ModelSerializerand want to add a custom field which is not part of my model.

我创建了一个ModelSerializer并想添加一个不属于我的模型的自定义字段。

I found a description to add extra fields hereand I tried the following:

我在这里找到了添加额外字段的描述,我尝试了以下操作:

customField = CharField(source='my_field')

When I add this field and call my validate()function then this field is not part of the attrdict. attrcontains all model fields specified except the extra fields. So I cannot access this field in my overwritten validation, can I?

当我添加这个字段并调用我的validate()函数时,这个字段不是attr字典的一部分。attr包含除额外字段外指定的所有模型字段。所以我无法在覆盖验证中访问此字段,可以吗?

When I add this field to the field list like this:

当我将此字段添加到字段列表时,如下所示:

class Meta:
    model = Account
    fields = ('myfield1', 'myfield2', 'customField')

then I get an error because customFieldis not part of my model - what is correct because I want to add it just for this serializer.

然后我得到一个错误,因为customField它不是我的模型的一部分 - 什么是正确的,因为我只想为这个序列化程序添加它。

Is there any way to add a custom field?

有没有办法添加自定义字段?

采纳答案by Tom Christie

You're doing the right thing, except that CharField(and the other typed fields) are for writable fields.

您正在做正确的事情,除了CharField(和其他类型的字段)用于可写字段。

In this case you just want a simple read-only field, so instead just use:

在这种情况下,您只需要一个简单的只读字段,因此只需使用:

customField = Field(source='get_absolute_url')

回答by va-dev

here answer for your question. you should add to your model Account:

在这里回答你的问题。您应该添加到您的模型帐户:

@property
def my_field(self):
    return None

now you can use:

现在你可以使用:

customField = CharField(source='my_field')

source: https://stackoverflow.com/a/18396622/3220916

来源:https: //stackoverflow.com/a/18396622/3220916

回答by Lindauson

...for clarity, if you have a Model Method defined in the following way:

...为清楚起见,如果您按以下方式定义了模型方法:

class MyModel(models.Model):
    ...

    def model_method(self):
        return "some_calculated_result"

You can add the result of calling said method to your serializer like so:

您可以将调用所述方法的结果添加到序列化程序中,如下所示:

class MyModelSerializer(serializers.ModelSerializer):
    model_method_field = serializers.CharField(source='model_method')

p.s. Since the custom field isn't really a field in your model, you'll usually want to make it read-only, like so:

ps 由于自定义字段实际上并不是模型中的字段,因此您通常希望将其设为只读,如下所示:

class Meta:
    model = MyModel
    read_only_fields = (
        'model_method_field',
        )

回答by Guillaume Vincent

With the last version of Django Rest Framework, you need to create a method in your model with the name of the field you want to add.

使用最新版本的 Django Rest Framework,您需要在模型中使用要添加的字段的名称创建一个方法。

class Foo(models.Model):
    . . .
    def foo(self):
        return 'stuff'
    . . .

class FooSerializer(ModelSerializer):
    foo = serializers.ReadOnlyField()

    class Meta:
        model = Foo
        fields = ('foo',)

回答by Fran?ois Constant

To show self.author.full_name, I got an error with Field. It worked with ReadOnlyField:

为了表明self.author.full_name,我遇到了一个错误Field。它与ReadOnlyField

class CommentSerializer(serializers.HyperlinkedModelSerializer):
    author_name = ReadOnlyField(source="author.full_name")
    class Meta:
        model = Comment
        fields = ('url', 'content', 'author_name', 'author')

回答by Idaho

In fact there a solution without touching at all the model. You can use SerializerMethodFieldwhich allow you to plug any method to your serializer.

事实上,有一个完全不涉及模型的解决方案。您可以使用SerializerMethodFieldwhich 允许您将任何方法插入序列化程序。

class FooSerializer(ModelSerializer):
    foo = serializers.SerializerMethodField()

    def get_foo(self, obj):
        return "Foo id: %i" % obj.pk

回答by David Schumann

I was looking for a solution for adding a writable custom field to a model serializer. I found this one, which has not been covered in the answers to this question.

我正在寻找一种将可写自定义字段添加到模型序列化程序的解决方案。我找到了这个,这个问题的答案中没有涉及到。

It seems like you do indeed need to write your own simple Serializer.

看起来您确实需要编写自己的简单序列化程序。

class PassThroughSerializer(serializers.Field):
    def to_representation(self, instance):
        # This function is for the direction: Instance -> Dict
        # If you only need this, use a ReadOnlyField, or SerializerField
        return None

    def to_internal_value(self, data):
        # This function is for the direction: Dict -> Instance
        # Here you can manipulate the data if you need to.
        return data

Now you can use this Serializer to add custom fields to a ModelSerializer

现在您可以使用此 Serializer 将自定义字段添加到 ModelSerializer

class MyModelSerializer(serializers.ModelSerializer)
    my_custom_field = PassThroughSerializer()

    def create(self, validated_data):
        # now the key 'my_custom_field' is available in validated_data
        ...
        return instance

This also works, if the Model MyModelactually has a property called my_custom_fieldbut you want to ignore its validators.

如果模型MyModel实际上有一个被调用的属性,my_custom_field但你想忽略它的验证器,这也有效。