當試圖將實例添加到我的某個模型時,管理員返回此錯誤。該模型本身有一個正確的str()方法,並且不包含實例。還嘗試用靜態方法替換str()方法或完全刪除它。沒有運氣。管理中的錯誤:__str__返回的非字符串(類型爲NoneType)
該錯誤似乎指向管理員的歷史部分出現問題。堆棧跟蹤點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 %}
這是我models.py
和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']
和
admin.site.register(Linetrans)
其他模型類的相關部分都非常相似,並且不返回錯誤。當Linetrans作爲內聯添加到另一個管理類時,也會出現該錯誤。
編輯/更新:我在我的模型中註釋了所有其他的str()方法,並且肯定錯誤似乎消失了。現在試圖找出問題。
我有返回__str__方法'self.title '而不是'self.title或'''。 – Seth