我想在Django应用的管理面板中访问模型-http://127.0.0.1:8000/admin/scraper/ad_history/
当我单击模型链接以查看其条目时,出现以下错误:
TypeError at /admin/scraper/ad_history/
__str__ returned non-string (type bytes)
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/scraper/ad_history/
Django Version: 2.1
Exception Type: TypeError
Exception Value:
__str__ returned non-string (type bytes)
Exception Location: C:\Python36-32\lib\site-packages\django\contrib\admin\templatetags\admin_list.py in items_for_result, line 251
Python Executable: C:\Python36-32\Anaconda\python.exe
Python Version: 3.6.5
Python Path:
['C:\\MyApp\\MyApp',
'C:\\Python36-32\\python36.zip',
'C:\\Python36-32\\DLLs',
'C:\\Python36-32\\lib',
'C:\\Python36-32',
'C:\\Python36-32\\lib\\site-packages',
'C:\\Python36-32\\lib\\site-packages\\win32',
'C:\\Python36-32\\lib\\site-packages\\win32\\lib',
'C:\\Python36-32\\lib\\site-packages\\Pythonwin']
Server time: Tue, 21 Aug 2018 14:05:36 +0000
__str__ returned non-string (type bytes)
广告历史记录模型:
class Ad_History(models.Model):
ad = models.ForeignKey(Ad, on_delete=models.CASCADE)
date_found = models.DateTimeField(auto_now_add=True, blank=True)
keyword = models.ForeignKey(Keyword, null=True, on_delete=models.CASCADE)
kw_scrape_count = models.IntegerField()
page_found = models.IntegerField(default=None, null=True)
sequence = models.IntegerField(default=None, null=True)
def __str__(self):
return self.ad.description
Admin.py:
from django.contrib import admin
from .models import Domain, Ad, Phone, Email, Hour, Address, Ad_History
from scraper.forms import AdHistoryAdmin, AdAdmin, DomainAdmin
# Register your models here.
admin.site.register(Domain, DomainAdmin)
admin.site.register(Phone)
admin.site.register(Email)
admin.site.register(Address)
admin.site.register(Hour)
admin.site.register(Ad, AdAdmin)
admin.site.register(Ad_History, AdHistoryAdmin)
AdHistoryAdmin:
class AdHistoryAdmin(admin.ModelAdmin):
list_display = ('ad', 'date_found', 'kw_scrape_count', 'keyword', 'sequence')
我试图更改模型中的 str 函数以返回虚拟字符串(以确保没有字节被返回),但这仍然会产生错误。
有什么想法可以使我深究吗?
答案 0 :(得分:1)
我不确定您要输入什么到字符串中,但是使用非字符串值可以做到这一点:
def __str__(self):
return 'Value1={0}, Value2={1}'.format(self.value1, self.value2)