栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将模型中未包含的字段添加到Django REST框架中的序列化器

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将模型中未包含的字段添加到Django REST框架中的序列化器

如果有人好奇,解决方案是重写restore_object方法,并在实例化后将额外的实例变量添加到注释对象:

def restore_object(self, attrs, instance=None):        if instance is not None: instance.email = attrs.get('email', instance.email) instance.author = attrs.get('author', instance.author) instance.url = attrs.get('url', instance.url) instance.content = attrs.get('content', instance.content) instance.ip = attrs.get('ip', instance.ip) instance.post_title = attrs.get('post_title', instance.post_title) instance.post_url = attrs.get('post_url', instance.post_url) return instance        commenter_pw = attrs.get('commenter_pw')        del attrs['commenter_pw']        comment = Comment(**attrs)        comment.commenter_password = commenter_pw        return comment


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/647172.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号