是否已在最新版本中删除了Django Rest Framework中ModelSerializer的get_validation_exclusions方法? [英] Is get_validation_exclusions method of ModelSerializer in Django Rest Framework removed in latest versions?

查看:41
本文介绍了是否已在最新版本中删除了Django Rest Framework中ModelSerializer的get_validation_exclusions方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行以下代码时:

class PostSerializer(serializers.ModelSerializer):
    author = UserSerializer(required=False)

    def get_validation_exclusions(self):
        exclusions = super(PostSerializer, self).get_validation_exclusions()
        return exclusions + ['author']

我收到错误作者列不能为空。当我在线检查文档时,此方法从3.0版本开始不可用(链接: http ://www.cdrf.co/3.3/rest_framework.serializers/ModelSerializer.html )。请让我知道最新版本支持此方法的替代方法。

I am getting the error Column 'author' cannot be null. When I checked online for documentation, this method is not available since 3.0 release (link: http://www.cdrf.co/3.3/rest_framework.serializers/ModelSerializer.html). Please let me know the alternative for this method supported in latest version.

推荐答案

您可能希望设置 allow_null = True required = False 仅在根本没有定义密钥的情况下才起作用。

You'll likely want to set the allow_null=True. required=False will only work if the key isn't defined at all.

这篇关于是否已在最新版本中删除了Django Rest Framework中ModelSerializer的get_validation_exclusions方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆