我有一個奇怪的錯誤:Django管理錯誤而刪除
TypeError at /admin/wall/articles/
__str__ returned non-string (type Articles)
我有一個模型文章。 我只是試圖從Django的管理面板中刪除我的數據庫的一些文章。我說它很好奇,因爲它只發生在一些物體上。我可以刪除大部分文章,但由於某些原因,如果我刪除它們,數據庫中的某些條目會返回錯誤。這不適用於我的其他模型。
這是phpMyAdmin的顯示在我的桌子「公司章程」的所有條目的截圖:
例如,我有ID 70條目在Django管理潘內爾我無法刪除它,我有一個錯誤。爲了測試重新創建確切的條目(請參閱ID 75),我可以從django admin pannel中刪除此條目。
爲什麼我可以刪除一些內容但不是全部?
這是我的models.py:
from django.db import models
from start.models import Games
from django.contrib.auth.models import User
from tinymce.models import HTMLField
# Create your models here.
def user_directory_path_articles(instance, filename):
return 'media/user/user_{0}/articles/{1}/'.format(instance.user.id, filename)
def user_directory_path_comments(instance, filename):
return 'media/user/user_{0}/comments/{1}/'.format(instance.user.id, filename)
def validate_file_extension(value):
import os
from django.core.exceptions import ValidationError
ext = os.path.splitext(value.name)[1] # [0] returns path+filename
valid_extensions = ['.jpg', '.png']
if not ext.lower() in valid_extensions:
raise ValidationError(u'Merci d\'utiliser le format JPG ou PNG')
class Articles(models.Model):
title = models.CharField(max_length=50, null=False, verbose_name="Titre")
text = HTMLField()
image = models.FileField(upload_to='media/articles/', validators=[validate_file_extension], blank=True, null=True, verbose_name="Image de présentation")
games = models.ForeignKey(Games, verbose_name="Jeux", blank=True, null=True)
author = models.ForeignKey(User, verbose_name="Auteur")
is_statut = models.BooleanField(default=True, verbose_name="Statut")
date = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name="Date de création")
update = models.DateTimeField(auto_now=True, verbose_name="Dernière modification")
def __str__(self):
return self.title
class Comments(models.Model):
text = models.CharField(max_length=300, null=False)
image = models.FileField(upload_to=user_directory_path_comments, validators=[validate_file_extension], blank=True, null=True)
articles = models.ForeignKey(Articles, verbose_name="Article", null=False, related_name='comments')
author = models.ForeignKey(User, verbose_name="Auteur")
in_answer_to = models.ForeignKey('self', verbose_name="En réponse au commentaire", blank=True, null=True, on_delete=models.CASCADE)
date = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name="Date de création")
update = models.DateTimeField(auto_now=True, verbose_name="Dernière modification")
def __str__(self):
return self.text
class Like(models.Model):
articles = models.ForeignKey(Articles, verbose_name="Article", null=True, blank=True)
user = models.ForeignKey(User, verbose_name="Auteur", null=False)
def __str__(self):
return self.articles
class Up(models.Model):
comments = models.ForeignKey(Comments, verbose_name="Commentaire", null=True, blank=True)
user = models.ForeignKey(User, verbose_name="Auteur", null=False)
這是我的admin.py:
from django.contrib import admin
from .models import Articles, Comments
# Register your models here.
class ArticlesAdmin(admin.ModelAdmin):
list_display = ('date', 'title', 'author', 'games', 'is_statut', 'update')
fieldsets = (
('Général', {
'fields': ('title', 'author', 'is_statut')
}),
('Choisir une image de présentation', {
'fields': ('image',)}),
('Contenu de l\'article', {
'fields': ('text',)}),
('En rapport avec le jeu :', {
'fields': ('games',)}),
)
admin.site.register(Articles, ArticlesAdmin)
這是我的錯誤頁面的追蹤,並感謝您的幫助:
Environment:
Request Method: POST
Request URL: http://*******:8000/admin/wall/articles/
Django Version: 1.11.2
Python Version: 3.5.3
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'widget_tweaks',
'tinymce',
'start',
'myaccount',
'wall']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/options.py" in wrapper
551. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py" in _wrapped_view
149. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
57. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/sites.py" in inner
224. return view(request, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py" in _wrapper
67. return bound_func(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py" in _wrapped_view
149. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py" in bound_func
63. return func.__get__(self, type(self))(*args2, **kwargs2)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/options.py" in changelist_view
1568. response = self.response_action(request, queryset=cl.get_queryset(request))
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/options.py" in response_action
1286. response = func(self, request, queryset)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/actions.py" in delete_selected
37. queryset, opts, request.user, modeladmin.admin_site, using)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/utils.py" in get_deleted_objects
172. to_delete = collector.nested(format_callback)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/utils.py" in nested
232. roots.extend(self._nested(root, seen, format_callback))
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/utils.py" in _nested
216. children.extend(self._nested(child, seen, format_callback))
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/utils.py" in _nested
218. ret = [format_callback(obj)]
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/utils.py" in format_callback
145. force_text(obj))
File "/usr/local/lib/python3.5/dist-packages/django/utils/encoding.py" in force_text
76. s = six.text_type(s)
Exception Type: TypeError at /admin/wall/articles/
Exception Value: __str__ returned non-string (type Articles)
你有沒有其他模型的外鍵指向文章?如果是這樣,請發佈。 –
是的,我更新了我的帖子。這是我的整個models.py文件 – GrandGTO