我使用django 2.0将django_message软件包安装到django应用程序中,并且在我的开发机器上运行时,当我尝试从Django管理控制台打开消息列表时,它在生产中引发以下错误。
TypeError: __str__ returned non-string (type NoneType)
[22/Mar/2019 15:08:12] ERROR [django.request:118] Internal Server Error: /admin/django_messages/message/
Traceback (most recent call last):
File "/opt/site_env/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/opt/site_env/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/opt/site_env/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/opt/site_env/lib/python3.6/site-packages/django/contrib/admin/options.py", line 574, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/opt/site_env/lib/python3.6/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/opt/site_env/lib/python3.6/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/opt/site_env/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 223, in inner
return view(request, *args, **kwargs)
File "/opt/site_env/lib/python3.6/site-packages/django/utils/decorators.py", line 62, in _wrapper
return bound_func(*args, **kwargs)
File "/opt/site_env/lib/python3.6/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/opt/site_env/lib/python3.6/site-packages/django/utils/decorators.py", line 58, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "/opt/site_env/lib/python3.6/site-packages/django/contrib/admin/options.py", line 1570, in changelist_view
cl = self.get_changelist_instance(request)
File "/opt/site_env/lib/python3.6/site-packages/django/contrib/admin/options.py", line 705, in get_changelist_instance
self,
File "/opt/site_env/lib/python3.6/site-packages/django/contrib/admin/views/main.py", line 76, in __init__
self.queryset = self.get_queryset(request)
File "/opt/site_env/lib/python3.6/site-packages/django/contrib/admin/views/main.py", line 320, in get_queryset
filters_use_distinct) = self.get_filters(request)
File "/opt/site_env/lib/python3.6/site-packages/django/contrib/admin/views/main.py", line 130, in get_filters
self.model, self.model_admin, field_path=field_path
File "/opt/site_env/lib/python3.6/site-packages/django/contrib/admin/filters.py", line 157, in create
return list_filter_class(field, request, params, model, model_admin, field_path=field_path)
File "/opt/site_env/lib/python3.6/site-packages/django/contrib/admin/filters.py", line 168, in __init__
self.lookup_choices = self.field_choices(field, request, model_admin)
File "/opt/site_env/lib/python3.6/site-packages/django/contrib/admin/filters.py", line 195, in field_choices
return field.get_choices(include_blank=False)
File "/opt/site_env/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 812, in get_choices
limit_choices_to)]
File "/opt/site_env/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 811, in <listcomp>
for x in rel_model._default_manager.complex_filter(
File "/opt/site_env/lib/python3.6/site-packages/django/utils/encoding.py", line 34, in smart_text
return force_text(s, encoding, strings_only, errors)
File "/opt/site_env/lib/python3.6/site-packages/django/utils/encoding.py", line 67, in force_text
s = str(s)
TypeError: __str__ returned non-string (type NoneType)
最大的担忧是,这不是引发错误的唯一模型。我从django-allauth获得了与SocialAccount相同的错误。
另一个值得关注的问题是,我认为我不应该修复此软件包中的代码,但是我别无其他。
还要注意,应用程序本身的任何模型都不会导致这些错误。 仅适用于django_allauth和django_messages。但是,该故障不会在我的开发机器中发生。 django_messages的mysql表为空(无记录)。 是否存在导致此类错误或其他原因的配置。 我在两个环境中都使用django 2.0和python 3.6。
答案 0 :(得分:0)
想通了。
事实证明,所有失败的模型都与用户模型有关,该用户模型的 str ()方法指向其数据库列为NULL的字段。
解决方案:我必须通过在pre_save阶段分配默认值来使该字段在数据库级别和Django级别不为空。
有效。