管理员错误:__str__ 返回非字符串(类型 NoneType) [英] Error in admin: __str__ returned non-string (type NoneType)

查看:32
本文介绍了管理员错误:__str__ 返回非字符串(类型 NoneType)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

管理员在尝试将实例添加到我的模型之一时返回此错误.模型本身有一个正确的 str() 方法并且还不包含任何实例.还尝试用静态方法替换 str() 方法或将其完全删除.没有运气.

该错误似乎表明管理员的历史记录部分出了问题.Stacktrace 指向第 33 行.

模板渲染时出错在模板/Users/snirp/juis/snirpdrive/glotto/venv/lib/python3.6/site-packages/django/contrib/admin/templates/admin/change_form.html 中,第 33 行出错__str__ 返回非字符串(类型 NoneType)23 {% 端块 %}24 {% endif %}2526 {% 块内容 %}

27 {% 块对象工具 %}28 {% if change %}{% if not is_popup %}29 <ul class="object-tools">30 {% 块对象-工具-项目 %}31 <li>32 {% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %}33 <a href="{% add_preserved_filters history_url %}" class="historylink">{% trans "History" %}</a>34 </li>35 {% if has_absolute_url %}<li><a href="{{ absolute_url }}" class="viewsitelink">{% trans "View on site" %}</a></li>{% 万一 %}36 {% 端块 %}37 </ul>38 {% endif %}{% endif %}39 {% 端块 %}40 <form {% if has_file_field %}enctype="multipart/form-data" {% endif %}action="{{ form_url }}" method="post" id="{{ opts.model_name }}_form"novalidate>{% csrf_token %}{% block form_top %}{% endblock %}41 <div>42 {% if is_popup %}{% endif %}43 {% if to_field %}<input type="hidden" name="{{ to_field_var }}" value="{{ to_field }}"/>{% endif %}

这些是我的 models.pyadmin.py

的相关部分

class UserContent(models.Model):created_at = models.DateTimeField(auto_now_add=True)updated_at = models.DateTimeField(auto_now=True)created_by = models.ForeignKey(User, related_name='%(class)s_creator')updated_by = models.ForeignKey(User, related_name='%(class)s_updater')元类:抽象 = 真类 Linetrans(UserContent):line = models.ForeignKey(Line)翻译 = 模型.ForeignKey(翻译)text = models.CharField(max_length=400)#def __str__(self):# 返回 self.text元类:排序 = ['行']

admin.site.register(Linetrans)

其他模型类非常相似,不会返回错误.当 Linetrans 作为内联添加到另一个管理类时,也会发生该错误.

编辑/更新:我注释掉了模型中的所有其他 str() 方法,果然错误似乎消失了.现在试图查明问题.

解决方案

原来在相关模型中有一个意外的空 CharField.将此作为答案,因为它可能对其他人有所帮助.

解决问题,系统地注释掉模型的 __str__() 方法,直到找到有问题的模型.从那里开始工作以识别违规记录.

The admin returns this error when trying to add an instance to one of my models. The model itself has a correct str() method and contains no instances yet. Also tried replacing the str() method with a static method or removing it altogether. No luck.

The error seems to point towards something going wrong in the history part of the admin. Stacktrace points to line 33.

Error during template rendering

In template /Users/snirp/juis/snirpdrive/glotto/venv/lib/python3.6/site-packages/django/contrib/admin/templates/admin/change_form.html, error at line 33
__str__ returned non-string (type NoneType)
23  {% endblock %}
24  {% endif %}
25  
26  {% block content %}<div id="content-main">
27  {% block object-tools %}
28  {% if change %}{% if not is_popup %}
29    <ul class="object-tools">
30      {% block object-tools-items %}
31      <li>
32          {% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %}
33          <a href="{% add_preserved_filters history_url %}" class="historylink">{% trans "History" %}</a>
34      </li>
35      {% if has_absolute_url %}<li><a href="{{ absolute_url }}" class="viewsitelink">{% trans "View on site" %}</a></li>{% endif %}
36      {% endblock %}
37    </ul>
38  {% endif %}{% endif %}
39  {% endblock %}
40  <form {% if has_file_field %}enctype="multipart/form-data" {% endif %}action="{{ form_url }}" method="post" id="{{ opts.model_name }}_form" novalidate>{% csrf_token %}{% block form_top %}{% endblock %}
41  <div>
42  {% if is_popup %}<input type="hidden" name="{{ is_popup_var }}" value="1" />{% endif %}
43  {% if to_field %}<input type="hidden" name="{{ to_field_var }}" value="{{ to_field }}" />{% endif %}

These are the relevant parts of my models.py and admin.py

class UserContent(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    created_by = models.ForeignKey(User, related_name='%(class)s_creator')
    updated_by = models.ForeignKey(User, related_name='%(class)s_updater')

    class Meta:
        abstract = True


class Linetrans(UserContent):
    line = models.ForeignKey(Line)
    translation = models.ForeignKey(Translation)
    text = models.CharField(max_length=400)

    #def __str__(self):
    #    return self.text

    class Meta:
        ordering = ['line']

and

admin.site.register(Linetrans)

Other model classes are very similar and do not return an error. The error also occurs when the Linetrans is added as an inline to another admin class.

edit / update: I commented out all other str() methods in my model and sure enough the error seems to go away. Now trying to pinpoint the issue.

解决方案

Turns out that there was an unexpected empty CharField in a related model. Leaving this an an answer, because it might help others.

Troubleshoot the issue by systematically commenting out the __str__() methods of your models until you find the offending model. Work from there to identify the offending record(s).

这篇关于管理员错误:__str__ 返回非字符串(类型 NoneType)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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