Django报错 The serializer field might be named incorrectly and not match any Got AttributeError when attempting to get a value for field `author_for` on serializer `KnownledgeBaseListSerializer`
1、问题描述,在设置,model部分字段的serialier时,出现如下报错
字段如下:
# 知识库List
class KnownledgeBaseListSerializer(serializers.ModelSerializer):
article_state = serializers.CharField(source='get_article_state_display')
know_classify = serializers.CharField(source='know_classify.name')
knowledge_source = serializers.CharField(source='knowledge_source.name',write_only=True)
author_for = serializers.CharField(source='author_for.name')
class Meta:
model = KnowledgeBase
fields = ['theme_for','article_state','know_classify','knowledge_source','author_for','create_time','average_score']
2、问题原因:
1. 当场景是前端post数据过来,调用到了serializer实例的save()方法的时候,会将字段保存入库,但是数据库里面没有这个字段,所以以错误形式返回。解决如下:
def validate(self, attrs):
"""
attrs : 每个字段validate之后,返回的总的字段的dict
"""
del attrs["code"]
return attr
例中code就是model外的自定义字段,从序列化验证完成后返回的attrs中删除自定义”code”,就不会再保存这个字段
- 当场景为前端post数据过来进行实例创建的,比方说注册一个账户或者添加一个收藏,这个过程,将会调用到createmodelmixin的create方法:
class CreateModelMixin(object):
"""
Create a model instance.
"""
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
create最后在返回response体的时候,传递的参数serializer.data,其实做的是一个序列化的工作,它会依据你在class Meta里面设置的fields,去做序列化,将里面所有字段进行序列化,这个时候就会报错
3、解决方法如下:
code = serializers.CharField(max_length=6,min_length=6,required=True,
write_only=True,)
4、字段的write_only属性.
Set this to True to ensure that the field may be used when updating or creating an instance, but is not included when serializing the representation.
官方文档的说明是:设置这个属性为true,去确保create/update的时候这个字段被用到,序列化的时候,不被用到
参考:https://blog.csdn.net/Newbietan/article/details/80657202
Django报错 The serializer field might be named incorrectly and not match any Got AttributeError when attempting to get a value for field `author_for` on serializer `KnownledgeBaseListSerializer`的更多相关文章
- AttributeError: Got AttributeError when attempting to get a value for field `password2` on serializer ` UserSerializer`...
Error_msg: AttributeError: Got AttributeError when attempting to get a value for field `password2` o ...
- Django报错:OSError: raw write() returned invalid length 4 (should have been between 0 and 2)
在使用Django时Django报错:Django报错:OSError: raw write() returned invalid length 4 (should have been between ...
- Django报错:提交表单报错---RuntimeError: You called this URL via POST, but the URL doesn’t end in a slash and you have APPEND_SLASH set.
Django报错:提交表单报错---RuntimeError: You called this URL via POST, but the URL doesn’t end in a slash and ...
- django 报错Reverse for 'detail' with keyword arguments '{'pk': '2'}' not found. 1 pattern(s) tried: ['$post/(?P<pk>[0-9]+)/$']
Django报错:Reverse for 'detail' with keyword arguments '{'pk': '2'}' not found. 1 pattern(s) tried: [' ...
- Django 报错 Reverse for 'content' not found. 'content' is not a valid view function or pattern name.
Django 报错 Reverse for 'content' not found. 'content' is not a valid view function or pattern name. 我 ...
- ubuntu pip 安装django报错解决
系统版本 ubuntu Kylin 16.04 LTS 安装pip3 安装 Django 总是提示time out,无法安装. 逛了好多论坛终于遭到了解决办法,分享保存: sudo pi ...
- 关于Django报错django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configure
报错代码:django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but se ...
- 记一次Django报错Reverse for 'indextwo' with no arguments not found. 1 pattern(s) tried: ['$index/$']
启动python manage.py runserver 打开127.0.0.1:8000,报错信息如下: Reverse for 'indextwo' with no arguments not f ...
- django报错解决:view must be a callable or a list/tuple in the case of include().
django版本:1.11.15 django应用,修改urls.py后,访问报错:TypeError at /view must be a callable or a list/tuple in t ...
随机推荐
- 【算法学习记录-排序题】【PAT A1025】PAT Ranking
Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhe ...
- sql简单练习语句
排序是每个软件工程师和开发人员都需要掌握的技能.不仅需要通过编程面试,还要对程序本身有一个全面的理解.不同的排序算法很好地展示了算法设计上如何强烈的影响程序的复杂度.运行速度和效率. 排序有很多种实现 ...
- youtube-dl parameters
how to select the format of vidoes from youtube-dl? https://www.jianshu.com/p/611009843919 https://u ...
- 在PDB级别中如何切换或重建UNDO表空间
Oracle 12.1版本中,UNDO表空间仅存在CDB级别(共享UNDO),来自于AskScuti博客园. Oracle 12.2版本开始,UNDO表空间同时可以存在每个PDB级别(本地UNDO). ...
- SQL语句分类和语法
DQL:Data QueryLanguage 数据查询语言 作用: 查询表中的字段 命令: select 查询 ⑦select 查询列表①from 表1 别名②连接类型 joi ...
- 2020ICPC 博弈题 纳新一百的石子游戏
https://ac.nowcoder.com/acm/contest/4010/C 这道题为尼姆博弈的其中一种裸类型: 要求求出前(1~n)堆的必胜方案. 对于这种类型,假如我们现在就前k堆,那么我 ...
- Linux - Shell - shell 执行方式
概述 shell 的执行方式 背景 偶尔执行个 shell 脚本 一般都用 './script' 执行 最近忽然看到 有不同的执行方式, 感觉有必要整理一下, 然后和大家分享 准备 os centos ...
- hive导出数据到本地文件报错解决方法
报错信息: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.MoveTask. Unable to move so ...
- JS高级---面向对象的编程思想(贪吃蛇梳理)
面向对象的编程思想(贪吃蛇梳理) 模拟贪吃蛇游戏,做的项目 地图: 宽,高,背景颜色,因为小蛇和食物都是相对于地图显示的, 这里小蛇和食物都是地图的子元素, 随机位置显示, 脱离文档流的, 地图也需要 ...
- python 变量的赋值【内存地址】
注意: python所有的数据都是对象,变量只是指向一个对象的地址,一旦将变量的值或者类型改变,变量指向的地址就有可能发生变化 这个特性在使用默认参数的时候一定要注意