Python 在 DRF 3 中的 ModelSerializer 上添加非模型字段

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

Add a non-model field on a ModelSerializer in DRF 3

pythondjangodjango-rest-framework

提问by Prometheus

How do add a non-model field on a ModelSerializer in DRF 3? i.e. add a field that does not exist on my actual model?

如何在 DRF 3 中的 ModelSerializer 上添加非模型字段?即添加一个在我的实际模型中不存在的字段?

class TestSerializer(serializers.ModelSerializer):
    url = serializers.HyperlinkedIdentityField(view_name='vote_detail')
    non_field = serializers.CharField()  # no corresponding model property.


    class Meta:
        model = vote_model
        fields = ("url", "non_field")

    def create(self, validated_data):
      print(direction=validated_data['non_field'])

But DRF 3 gives me the error:

但是 DRF 3 给了我错误:

Got AttributeError when attempting to get a value for field `non_field` on serializer `TestSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `Test` instance.
Original exception text was: 'Test' object has no attribute 'non_field'.

I have searched stack DRF - ModelSerializer with a non-model write_only fieldand found a few solutions but these refer to DRF 2 where I'm using DRF 3. Is there a solution for this on this version?

我已经用非模型 write_only 字段搜索了堆栈DRF - ModelSerializer并找到了一些解决方案,但这些解决方案是指我使用 DRF 3 的 DRF 2。在这个版本上有解决方案吗?

采纳答案by chandu

class TestSerializer(serializers.ModelSerializer):
    url = serializers.HyperlinkedIdentityField(view_name='vote_detail')
    non_field = serializers.SerializerMethodField()  # no corresponding model property.

    class Meta:
        model = vote_model
        fields = ("url", "non_field")

    def create(self, validated_data):
        print(direction=validated_data['non_field'])

http://www.django-rest-framework.org/api-guide/fields/#serializermethodfield

http://www.django-rest-framework.org/api-guide/fields/#serializermethodfield

or go through this link

或通过此链接

回答by chandu

Just an example might help you.

只是一个例子可能对你有帮助。

  class ExtensibleModelSerializerOptions(serializers.SerializerOptions):
    """
    Meta class options for ModelSerializer
    """
    def __init__(self, meta):
        super(ExtensibleModelSerializerOptions, self).__init__(meta)
        self.model = getattr(meta, 'model', None)
        self.read_only_fields = getattr(meta, 'read_only_fields', ())
        self.non_native_fields = getattr(meta, 'non_native_fields', ())


class ExtensibleModelSerializer(serializers.ModelSerializer):

    _options_class = ExtensibleModelSerializerOptions

    def restore_object(self, attrs, instance=None):
        """
        Deserialize a dictionary of attributes into an object instance.
        You should override this method to control how deserialized objects
        are instantiated.
        """
        for field in self.opts.non_native_fields:
            attrs.pop(field)

        return super(ExtensibleModelSerializer, self).restore_object(attrs, instance)

Source: https://github.com/tomchristie/django-rest-framework/issues/951

来源:https: //github.com/tomchristie/django-rest-framework/issues/951

回答by chandu

class Foo(models.Model):
    . . .
    @property
    def my_field(self):
        return stuff
    . . .

Source:

来源:

Django REST Framework: adding additional field to ModelSerializer

Django REST Framework:向 ModelSerializer 添加附加字段

回答by A. J. Parr

class MySerializer(serializers.ModelSerializer):
    write_only_char_field = serializers.CharField(write_only=True)
    write_only_list_char_field = serializers.ListField(child=serializers.CharField(max_length=100, default=''), write_only=True)
    empty_method_field = serializers.SerializerMethodField()
    read_only_custom_model_field = serializers.CharField(source='custom_property', read_only=True)

    def create(self, validated_data):
        validated_data.pop('write_only_char_field', None)
        validated_data.pop('write_only_list_char_field', None)
        return super().create(validated_data)

The serializers.CharField(write_only=True)and serializers.ListField(...)is a good solution to provide extra data to your .create()and .update()methods, as either a single string or a list of strings (you can mix ListFieldwith other serializer field types).
With this method, you can also define def validate_write_only_char_fieldto implement some quick and simple validation.

serializers.CharField(write_only=True)serializers.ListField(...)是一个很好的解决方案,以提供额外的数据到你.create().update()方法,无论是作为单个字符串或字符串列表(你可以混合ListField与其他串行字段类型)。
使用此方法,您还可以定义def validate_write_only_char_field以实现一些快速简单的验证。

serializers.SerializerMethodField()allows you to add some custom read-only field to your serializer output from a method defined on the serializer.

serializers.SerializerMethodField()允许您从序列化程序上定义的方法向序列化程序输出添加一些自定义只读字段。

The read_only_custom_model_fieldwould use a method on your model to read some data, not strictly a model field, but a custom method. I.e.

read_only_custom_model_field会使用在你的模型的方法来读取一些数据,不是严格意义上的模型领域,但自定义方法。IE

class MyModel(models.Model):
    my_field = models.CharField(max_length=100)

    @property
    def custom_property(self):
        return "Perform calculations, combine with related models, etc. etc."

回答by Xen_mar

As mentioned there are two ways. (1) adding a model property. (2) adding a model field. I feel that adding a @property to model was explained well in this post. If you want to keep your models "lean and mean" use a Method field. Chandus answer omits some crucial points, though:

如前所述,有两种方法。(1) 添加模型属性。(2) 添加模型字段。我觉得在这篇文章中很好地解释了向模型添加@property。如果您想保持模型“精益求精”,请使用 Method 字段。不过,Chandus 的回答忽略了一些关键点:

class DeliveryItemSerializer(serializers.ModelSerializer):

    product_name = serializers.SerializerMethodField(read_only=True)

    def get_product_name(self, obj):
        return obj.product.name

    class Meta:
        model = DeliveryItem
        fields = (
            (...your field names),
            'product_name',)
  1. Set read_only
  2. The field and the corresponding method do not have the same name.The method name defaults to get_field_name. If you use another name use the method_name=methodname argument on SerializerMethodField()
  1. 设置只读
  2. 该字段和相应的方法不具有相同的名称。方法名称默认为get_field_name。如果您使用其他名称,请使用method_name=method名称参数SerializerMethodField()