This is my Model:
这是我的模特:
class Post(models.Model):
user = models.ForeignKey(User)
post = models.CharField(max_length=400)
country = models.ForeignKey(Country, blank=True, null=True)
and this is my serializer:
这是我的序列化器:
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = ('user', 'post', 'country',)
def create(self, validated_data):
post = Post(
user = User.objects.get(username='MyUser'),
post = validated_data['post'],
)
if validated_data.get('country', None):
post.country = validated_data['country']
return post
Is there any way for me to tell DRF that if the value of the field is null (because the 'country' field is optional and sometimes not provided) then to skip it and just serialize the other data? Or at least serialize it with a value of None?
有没有办法让我告诉DRF,如果该字段的值为null(因为'country'字段是可选的,有时不提供),那么跳过它并只序列化其他数据?或者至少将其序列化为None值?
I don't think I can use SerializerMethodField (http://www.django-rest-framework.org/api-guide/fields/#serializermethodfield) because the 'country' field is not a read-only field (I write too it too, if it is provided).
我不认为我可以使用SerializerMethodField(http://www.django-rest-framework.org/api-guide/fields/#serializermethodfield),因为'country'字段不是只读字段(我也写过)它也是,如果提供的话)。
I basically want to omit the field (or at least make the value None) when serializing an object If the field is null.
我基本上想要在序列化对象时省略该字段(或至少使值为None)如果该字段为null。
2 个解决方案
#1
3
As of DRF 3.2.4, so long as you add
从DRF 3.2.4开始,只要添加即可
blank=True
to the models field like so:
到这样的模型领域:
class Post(models.Model):
country = models.ForeignKey(Country, blank=True)
then DRF will treat the field as optional when serializing and deserializing it (Note though that if there is no null=True on the model field, then Django will raise an error if you try to save an object to the database without providing the field).
然后,当序列化和反序列化时,DRF会将该字段视为可选字段(注意,如果模型字段上没有null = True,那么如果您尝试将对象保存到数据库而不提供字段,则Django将引发错误) 。
See the answer here for more information: DjangoRestFramework - correct way to add "required = false" to a ModelSerializer field?
有关更多信息,请参阅此处的答案:DjangoRestFramework - 将“required = false”添加到ModelSerializer字段的正确方法?
If you are using pre-DRF 3.2.4, then you can override the field in the serializer and add required=False to it. See the documentation here for more information on specifying or overriding fields explicitily: http://www.django-rest-framework.org/api-guide/serializers/#specifying-fields-explicitly
如果您使用pre-DRF 3.2.4,则可以覆盖序列化程序中的字段并为其添加required = False。有关明确指定或覆盖字段的更多信息,请参阅此处的文档:http://www.django-rest-framework.org/api-guide/serializers/#specifying-fields-explicitly
So something like this (Note that I did not fully test the code below but it should be something along these lines):
所以这样的事情(请注意,我没有完全测试下面的代码,但它应该是这些行的东西):
class PostSerializer(serializers.ModelSerializer):
country = serializers.PrimaryKeyRelatedField(required=False)
class Meta:
model = Post
fields = ('user', 'post', 'country',)
#2
2
This thread might be useful:
这个帖子可能很有用:
https://*.com/a/28870066/4698253
It basically says that you can override the to_representation() function with a slight modification.
它基本上表示你可以稍微修改一下覆盖to_representation()函数。
I would have put this in the comments but I don't have enough points yet :(
我会把这个放在评论中,但我还没有足够的分数:(
#1
3
As of DRF 3.2.4, so long as you add
从DRF 3.2.4开始,只要添加即可
blank=True
to the models field like so:
到这样的模型领域:
class Post(models.Model):
country = models.ForeignKey(Country, blank=True)
then DRF will treat the field as optional when serializing and deserializing it (Note though that if there is no null=True on the model field, then Django will raise an error if you try to save an object to the database without providing the field).
然后,当序列化和反序列化时,DRF会将该字段视为可选字段(注意,如果模型字段上没有null = True,那么如果您尝试将对象保存到数据库而不提供字段,则Django将引发错误) 。
See the answer here for more information: DjangoRestFramework - correct way to add "required = false" to a ModelSerializer field?
有关更多信息,请参阅此处的答案:DjangoRestFramework - 将“required = false”添加到ModelSerializer字段的正确方法?
If you are using pre-DRF 3.2.4, then you can override the field in the serializer and add required=False to it. See the documentation here for more information on specifying or overriding fields explicitily: http://www.django-rest-framework.org/api-guide/serializers/#specifying-fields-explicitly
如果您使用pre-DRF 3.2.4,则可以覆盖序列化程序中的字段并为其添加required = False。有关明确指定或覆盖字段的更多信息,请参阅此处的文档:http://www.django-rest-framework.org/api-guide/serializers/#specifying-fields-explicitly
So something like this (Note that I did not fully test the code below but it should be something along these lines):
所以这样的事情(请注意,我没有完全测试下面的代码,但它应该是这些行的东西):
class PostSerializer(serializers.ModelSerializer):
country = serializers.PrimaryKeyRelatedField(required=False)
class Meta:
model = Post
fields = ('user', 'post', 'country',)
#2
2
This thread might be useful:
这个帖子可能很有用:
https://*.com/a/28870066/4698253
It basically says that you can override the to_representation() function with a slight modification.
它基本上表示你可以稍微修改一下覆盖to_representation()函数。
I would have put this in the comments but I don't have enough points yet :(
我会把这个放在评论中,但我还没有足够的分数:(