I created a ModelSerializer
and want to add a custom field which is not part of my model.
我创建了一个ModelSerializer,并希望添加一个不属于模型的自定义字段。
I found a description to add extra fields here and 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 attr
dict. attr
contains all model fields specified except the extra fields. So I cannot access this field in my overwritten validation, can I?
当我添加这个字段并调用我的validate()函数时,这个字段不是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 customField
is 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?
有办法添加自定义字段吗?
6 个解决方案
#1
43
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')
#2
23
In fact there a solution without touching at all the model. You can use SerializerMethodField
which allow you to plug any method to your serializer.
事实上,有一种解决方案,完全不触及模型。您可以使用SerializerMethodField,它允许您将任何方法插入到序列化器中。
class FooSerializer(ModelSerializer):
foo = serializers.SerializerMethodField()
def get_foo(self, obj):
return "Foo id: %i" % obj.pk
#3
13
...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:
另外,由于自定义字段在您的模型中并不是一个字段,您通常希望它是只读的,如下所示:
class Meta:
model = MyModel
read_only_fields = (
'model_method_field',
)
#4
10
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://*.com/a/18396622/3220916
来源:https://*.com/a/18396622/3220916
#5
4
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框架的最后一个版本,您需要在模型中创建一个方法,该方法的名称是您想要添加的字段。
class Foo(models.Model):
. . .
def foo(self):
return 'stuff'
. . .
class FooSerializer(ModelSerializer):
foo = serializers.ReadOnlyField()
class Meta:
model = Foo
fields = ('foo',)
#6
3
To show self.author.full_name
, I got an error with Field
. It worked with ReadOnlyField
:
显示self.author。full_name,我有一个字段错误。它与ReadOnlyField合作:
class CommentSerializer(serializers.HyperlinkedModelSerializer):
author_name = ReadOnlyField(source="author.full_name")
class Meta:
model = Comment
fields = ('url', 'content', 'author_name', 'author')
#1
43
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')
#2
23
In fact there a solution without touching at all the model. You can use SerializerMethodField
which allow you to plug any method to your serializer.
事实上,有一种解决方案,完全不触及模型。您可以使用SerializerMethodField,它允许您将任何方法插入到序列化器中。
class FooSerializer(ModelSerializer):
foo = serializers.SerializerMethodField()
def get_foo(self, obj):
return "Foo id: %i" % obj.pk
#3
13
...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:
另外,由于自定义字段在您的模型中并不是一个字段,您通常希望它是只读的,如下所示:
class Meta:
model = MyModel
read_only_fields = (
'model_method_field',
)
#4
10
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://*.com/a/18396622/3220916
来源:https://*.com/a/18396622/3220916
#5
4
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框架的最后一个版本,您需要在模型中创建一个方法,该方法的名称是您想要添加的字段。
class Foo(models.Model):
. . .
def foo(self):
return 'stuff'
. . .
class FooSerializer(ModelSerializer):
foo = serializers.ReadOnlyField()
class Meta:
model = Foo
fields = ('foo',)
#6
3
To show self.author.full_name
, I got an error with Field
. It worked with ReadOnlyField
:
显示self.author。full_name,我有一个字段错误。它与ReadOnlyField合作:
class CommentSerializer(serializers.HyperlinkedModelSerializer):
author_name = ReadOnlyField(source="author.full_name")
class Meta:
model = Comment
fields = ('url', 'content', 'author_name', 'author')