gpt4 book ai didi

python - 如何跳过 DRF 序列化器 FloatField 的验证

转载 作者:行者123 更新时间:2023-12-01 00:27:51 24 4
gpt4 key购买 nike

问题

我需要添加将 minimum_commission 保存为正数的功能,但 None 也可用。我已经添加了序列化器:

class Appartement(models.Model):
minimum_commission = models.FloatField(null=True, default=None)

class RentalTestSerializer(serializers.ModelSerializer):
minimum_commission = serializers.FloatField(required=False, min_value=0)

class Meta:
model = Appartement
fields = (
'minimum_commission',
)

但是当我从表单(或 shell)传递空字符串时,我看到 A valid number is required 验证错误。

In [21]: ser = RentalTestSerializer(data={'minimum_commission': ''})

In [22]: ser.is_valid()
Out[22]: False

In [23]: ser.errors
Out[23]:
ReturnDict([('minimum_commission',
[ErrorDetail(string=u'A valid number is required.', code=u'invalid')])])

可能的解决方案

首先,我添加了自定义字段:BlankableFloatField,它将空字符串转换为 None:

class BlankableFloatField(serializers.FloatField):
"""
We wanted to be able to receive an empty string ('') or 'null' for a float field
and in that case turn it into a None number
"""
def to_internal_value(self, data):
if data in ['', 'null']:
return None

return super(BlankableFloatField, self).to_internal_value(data)

并添加了自定义验证:

class RentalTestSerializer(serializers.ModelSerializer):
minimum_commission = BlankableFloatField(required=False)

class Meta:
model = Appartement
fields = (
'minimum_commission',
)

def validate_minimum_commission(self, value):
if value and value < 0:
raise serializers.ValidationError(_("Minimum commission should be greater than 0"))
return value

目前它按预期工作:

In [38]: ser = RentalTestSerializer(data={'minimum_commission': ''})

In [39]: ser.is_valid()
Out[39]: True

但我认为有没有办法做得更优雅?

<小时/>

更新:

据我所知,主要问题是从表单中传递空字符串。我需要它来重置 minimum_commission 列。所以,最终的解决方案是:

class Appartement(models.Model):
# I have added the blank=True and validator
minimum_commission = models.FloatField(null=True, default=None, blank=True, validators=[MinValueValidator(0.0)])

class RentalTestSerializer(serializers.ModelSerializer):
# I continue to use the custom field to transform empty space to the None
# but instead of custom validation just added the allow_null=True and min_value validator.
minimum_commission = BlankableFloatField(required=False, allow_null=True, min_value=0)

class Meta:
model = Appartement
fields = (
'minimum_commission',
)
<小时/>

UPD2:

在了解了处理空字符串的解决方案后,我发现更清晰的是完全跳过在前端发送空字符串并将 default=None 设置到序列化器中。此外,不再需要自定义字段。

class Appartement(models.Model):
minimum_commission = models.FloatField(null=True, default=None)

class RentalTestSerializer(serializers.ModelSerializer):
minimum_commission = serializer.FloatField(required=False, allow_null=True, min_value=0, default=None)

class Meta:
model = Appartement
fields = (
'minimum_commission',
)

最佳答案

就像我在评论中提到的 - 公寓模型会很有用。

但是如果我理解正确的话,你想要实现的目标应该非常简单,如下所示:

class Appartement(models.Model):
minimum_commission = models.FloatField(blank=True, null=True, default=None)

class RentalTestSerializer(serializers.ModelSerializer):

class Meta:
model = Appartement
fields = (
'minimum_commission',
)

在此实现中,minimum_commission 是可选的,并且它也接受 null 值。

关于python - 如何跳过 DRF 序列化器 FloatField 的验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58416775/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com