如何在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'])
但是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'.
我已经搜索了堆栈Cassandra并找到了一些解决方案,但这些解决方案是指我正在使用DRF 3的DRF 2.这个版本是否有解决方案?
答案 0 :(得分:41)
serializers.CharField(write_only=True)
serializers.ListField(...)
和.create
是为.update
和ListField
方法提供额外数据的好方法,可以是单个字符串或字符串列表(您可以将def validate_write_only_char_field
与其他序列化程序字段类型混合使用。使用此方法,您还可以定义serializers.SerializerMethodField()
以实现一些快速简单的验证。
read_only_custom_model_field
允许您从序列化程序中定义的方法向序列化程序输出添加一些自定义只读字段。
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."
将使用模型上的方法来读取某些数据,而不是严格的模型字段,而是自定义方法。即。
npm install lodash --save
typings install lodash --ambient --save
答案 1 :(得分:6)
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
或完成此link
答案 2 :(得分:2)
只是一个例子可以帮助你。
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)
来源: https://github.com/tomchristie/django-rest-framework/issues/951
答案 3 :(得分:1)
class Foo(models.Model):
. . .
@property
def my_field(self):
return stuff
. . .
来源:
Django REST Framework: adding additional field to ModelSerializer
答案 4 :(得分:0)
如上所述,有两种方法。 (1)添加模型属性。 (2)添加模型字段。我觉得在这篇文章中很好地说明了如何在模型中添加@property。如果要使模型保持“精益求精”,请使用“方法”字段。但是,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',)
get_
field_name 。如果您使用其他名称,请在method_name=method
上使用SerializerMethodField()
name参数