python 异常记录

数据库异常记录

django.core.exceptions.ImproperlyConfigured: Field name transerver is not valid for model SysTask

日志信息如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
[2021-02-04 10:35:07] [log.py:log_response:228] ERROR Internal Server Error: /api/v2/migratetasks/sys_task/
Traceback (most recent call last):
File "/opt/virtual_env/backend_env/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/opt/virtual_env/backend_env/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/opt/virtual_env/backend_env/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/lib64/python3.6/contextlib.py", line 52, in inner
return func(*args, **kwds)
File "/opt/virtual_env/backend_env/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/opt/virtual_env/backend_env/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/opt/virtual_env/backend_env/lib/python3.6/site-packages/rest_framework/views.py", line 483, in dispatch
response = self.handle_exception(exc)
File "/opt/virtual_env/backend_env/lib/python3.6/site-packages/rest_framework/views.py", line 443, in handle_exception
self.raise_uncaught_exception(exc)
File "/opt/virtual_env/backend_env/lib/python3.6/site-packages/rest_framework/views.py", line 480, in dispatch
response = handler(request, *args, **kwargs)
File "/opt/virtual_env/backend_env/lib/python3.6/site-packages/rest_framework/generics.py", line 192, in post
return self.create(request, *args, **kwargs)
File "/opt/virtual_env/backend_env/lib/python3.6/site-packages/rest_framework/mixins.py", line 20, in create
serializer.is_valid(raise_exception=True)
File "/opt/virtual_env/backend_env/lib/python3.6/site-packages/rest_framework/serializers.py", line 236, in is_valid
self._validated_data = self.run_validation(self.initial_data)
File "/opt/virtual_env/backend_env/lib/python3.6/site-packages/rest_framework/serializers.py", line 434, in run_validation
value = self.to_internal_value(data)
File "/opt/virtual_env/backend_env/lib/python3.6/site-packages/rest_framework/serializers.py", line 482, in to_internal_value
fields = self._writable_fields
File "/opt/virtual_env/backend_env/lib/python3.6/site-packages/django/utils/functional.py", line 37, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/opt/virtual_env/backend_env/lib/python3.6/site-packages/rest_framework/serializers.py", line 370, in _writable_fields
field for field in self.fields.values() if not field.read_only
File "/opt/virtual_env/backend_env/lib/python3.6/site-packages/rest_framework/serializers.py", line 363, in fields
for key, value in self.get_fields().items():
File "/opt/virtual_env/backend_env/lib/python3.6/site-packages/rest_framework/serializers.py", line 1045, in get_fields
source, info, model, depth
File "/opt/virtual_env/backend_env/lib/python3.6/site-packages/rest_framework/serializers.py", line 1190, in build_field
return self.build_unknown_field(field_name, model_class)
File "/opt/virtual_env/backend_env/lib/python3.6/site-packages/rest_framework/serializers.py", line 1302, in build_unknown_field
(field_name, model_class.__name__)
django.core.exceptions.ImproperlyConfigured: Field name `transerver` is not valid for model `SysTask`.

原因分析:

串行器(serializers.ModeSerializer)中调用的models里面的字段不对而导致的,即在serializers的fields列表里引入了model中不存在的字段

1
2
3
4
5
6
7
class SysTaskSerializer(serializers.ModelSerializer):

class Meta:
model = SysTask
fields = ("id", "user", "task_name", "region_id", "zone_id", "zone_name",
"bandwidth_limit", "is_automatically", "account", "source",
"disk_list", "target_server", "transerver", "start_time")

例如上述序列化中,SysTask表中没有transerver字段,在序列化中引入就会引发上述错误

处理:

查看是否引入了不存在的字段,或引入的字段与model中定义的字段不同,删掉或修改即可

-------------本文结束感谢您的阅读-------------