我想序列化一个模型,但想包括一个额外的字段,需要在模型实例上做一些数据库查询要序列化:

class FooSerializer(serializers.ModelSerializer):
  my_field = ... # result of some database queries on the input Foo object
  class Meta:
        model = Foo
        fields = ('id', 'name', 'myfield')

正确的做法是什么?我看到你可以传递额外的“上下文”序列化器,是正确的答案,在上下文字典中传递额外的字段?

使用这种方法,获取我需要的字段的逻辑将不包含在序列化器定义中,这是理想的,因为每个序列化实例都需要my_field。在DRF序列化器文档的其他地方,它说“额外的字段可以对应于模型上的任何属性或可调用”。我说的是“额外字段”吗?

我是否应该在Foo的模型定义中定义一个返回my_field值的函数,并在序列化器中将my_field连接到该可调用对象?它看起来像什么?

如果有必要,我很乐意澄清问题。


当前回答

我认为SerializerMethodField是你要找的:

class FooSerializer(serializers.ModelSerializer):
  my_field = serializers.SerializerMethodField('is_named_bar')

  def is_named_bar(self, foo):
      return foo.name == "bar" 

  class Meta:
    model = Foo
    fields = ('id', 'name', 'my_field')

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

其他回答

您可以将模型方法更改为属性,并使用此方法在序列化器中使用它。

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

class FooSerializer(ModelSerializer):
    my_field = serializers.ReadOnlyField(source='my_field')

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

编辑:使用rest框架的最新版本(我尝试了3.3.3),您不需要更改属性。模型方法可以很好地工作。

如果你想读写额外的字段,你可以使用一个新的自定义序列化器,它扩展了序列化器。序列化器,像这样使用它

class ExtraFieldSerializer(serializers.Serializer):
    def to_representation(self, instance): 
        # this would have the same as body as in a SerializerMethodField
        return 'my logic here'

    def to_internal_value(self, data):
        # This must return a dictionary that will be used to
        # update the caller's validation data, i.e. if the result
        # produced should just be set back into the field that this
        # serializer is set to, return the following:
        return {
          self.field_name: 'Any python object made with data: %s' % data
        }

class MyModelSerializer(serializers.ModelSerializer):
    my_extra_field = ExtraFieldSerializer(source='*')

    class Meta:
        model = MyModel
        fields = ['id', 'my_extra_field']

我使用这个相关的嵌套字段与一些自定义逻辑

class Demo(models.Model):
    ...
    @property
    def property_name(self):
        ...

如果你想使用相同的属性名:

class DemoSerializer(serializers.ModelSerializer):
    property_name = serializers.ReadOnlyField()
    class Meta:
        model = Product
        fields = '__all__' # or you can choose your own fields

如果你想使用不同的属性名,只需更改这个:

new_property_name = serializers.ReadOnlyField(source='property_name')

我认为SerializerMethodField是你要找的:

class FooSerializer(serializers.ModelSerializer):
  my_field = serializers.SerializerMethodField('is_named_bar')

  def is_named_bar(self, foo):
      return foo.name == "bar" 

  class Meta:
    model = Foo
    fields = ('id', 'name', 'my_field')

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

尽管这不是作者想要的,但它仍然可以被认为对这里的人有用:

如果您正在使用.save() ModelSerializer的方法,您可以将**kwargs传递给它。这样,您就可以保存多个动态值。

储蓄(**)