Ron*_*Ron 79 python django django-rest-framework
我创建了一个ModelSerializer并且想要添加一个不属于我的模型的自定义字段.
我在这里找到了添加额外字段的说明,并尝试了以下内容:
customField = CharField(source='my_field')
Run Code Online (Sandbox Code Playgroud)
当我添加此字段并调用我的validate()函数时,此字段不是attrdict的一部分.attr包含除额外字段外指定的所有模型字段.所以我在覆盖验证中无法访问此字段,是吗?
当我将此字段添加到字段列表时,如下所示:
class Meta:
model = Account
fields = ('myfield1', 'myfield2', 'customField')
Run Code Online (Sandbox Code Playgroud)
然后我得到一个错误,因为customField它不是我的模型的一部分 - 什么是正确的,因为我想只为这个序列化器添加它.
有没有办法添加自定义字段?
Ida*_*aho 62
事实上,有一个解决方案没有触及任何模型.您可以使用SerializerMethodField哪个允许您将任何方法插入序列化程序.
class FooSerializer(ModelSerializer):
foo = serializers.SerializerMethodField()
def get_foo(self, obj):
return "Foo id: %i" % obj.pk
Run Code Online (Sandbox Code Playgroud)
Tom*_*tie 55
你正在做正确的事情,除了CharField(和其他类型的字段)是可写字段.
在这种情况下,您只需要一个简单的只读字段,所以只需使用:
customField = Field(source='get_absolute_url')
Run Code Online (Sandbox Code Playgroud)
Lin*_*son 14
...为清楚起见,如果您有以下列方式定义的模型方法:
class MyModel(models.Model):
...
def model_method(self):
return "some_calculated_result"
Run Code Online (Sandbox Code Playgroud)
您可以将调用所述方法的结果添加到序列化程序中,如下所示:
class MyModelSerializer(serializers.ModelSerializer):
model_method_field = serializers.CharField(source='model_method')
Run Code Online (Sandbox Code Playgroud)
ps由于自定义字段实际上不是模型中的字段,因此您通常希望将其设置为只读,如下所示:
class Meta:
model = MyModel
read_only_fields = (
'model_method_field',
)
Run Code Online (Sandbox Code Playgroud)
小智 13
在这里回答你的问题.您应该添加到您的模型帐户:
@property
def my_field(self):
return None
Run Code Online (Sandbox Code Playgroud)
现在你可以使用:
customField = CharField(source='my_field')
Run Code Online (Sandbox Code Playgroud)
来源:https://stackoverflow.com/a/18396622/3220916
Dav*_*han 11
我正在寻找一种将可写自定义字段添加到模型序列化器的解决方案。我找到了这个,这个问题的答案中没有涉及到这个。
看来您确实需要编写自己的简单序列化程序。
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
Run Code Online (Sandbox Code Playgroud)
现在您可以使用此序列化器将自定义字段添加到模型序列化器
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
Run Code Online (Sandbox Code Playgroud)
如果模型MyModel实际上有一个名为的属性my_custom_field,但您想忽略它的验证器,这也适用。
为了表明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')
Run Code Online (Sandbox Code Playgroud)
使用最新版本的Django Rest Framework,您需要在模型中创建一个方法,其中包含您要添加的字段的名称.
class Foo(models.Model):
. . .
def foo(self):
return 'stuff'
. . .
class FooSerializer(ModelSerializer):
foo = serializers.ReadOnlyField()
class Meta:
model = Foo
fields = ('foo',)
Run Code Online (Sandbox Code Playgroud)
在阅读了这里的所有答案后,我的结论是不可能干净利落地做到这一点。你必须玩得很脏,做一些像创建一个 write_only 字段,然后覆盖validate和to_representation方法之类的事情。这对我有用:
class FooSerializer(ModelSerializer):
foo = CharField(write_only=True)
class Meta:
model = Foo
fields = ["foo", ...]
def validate(self, data):
foo = data.pop("foo", None)
# Do what you want with your value
return super().validate(data)
def to_representation(self, instance):
data = super().to_representation(instance)
data["foo"] = whatever_you_want
return data
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
59250 次 |
| 最近记录: |