【问题标题】:ModelSerializer doesn't respect the required attribute on a fieldModelSerializer 不尊重字段的 required 属性
【发布时间】:2018-12-06 01:11:43
【问题描述】:

如果我有:

class Example(serializers.ModelSerializer):
    time = DateTimeRangeField(required=False)

它将失败并出现错误:

{'time': ['This field is required.']}

但是,如果这是 serializers.Serializer 它尊重字段上的必需属性。有没有其他人遇到过这个? 关于如何使字段不需要的任何想法?

【问题讨论】:

  • 我认为您需要为模型中的时间字段设置blank = True
  • 展示你的模型,序列化器就像一个表单,它只是验证请求数据,模型中的时间字段必须传递blank=Truenull=True
  • @Satendra,我试过了,它给出了同样的信息

标签: python django django-rest-framework django-serializer


【解决方案1】:

DateTimeRangeField 不是 Django REST 框架字段,所以就像您根本没有提供任何字段一样。

【讨论】:

    猜你喜欢
    • 2016-01-05
    • 2011-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-31
    • 1970-01-01
    • 2016-09-23
    • 2021-11-12
    相关资源
    最近更新 更多