DannyMoshe
DannyMoshe

Reputation: 6265

Error in Django Admin Site during template rendering- '__str__ returned non-string (type bytes)'

I would like to access a model in the admin panel of my Django app- http://127.0.0.1:8000/admin/scraper/ad_history/

When i click the model link to view its entries, i get the following error:

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)

Ad History model:

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')

I tried to change the str function in the model to return a dummy string (to ensure there were no bytes being returned) and this still generated the error.

Any ideas how i can get to the bottom of this?

Upvotes: 0

Views: 416

Answers (1)

Christo
Christo

Reputation: 31

I'm not sure what you are trying to input into a string but with non string values this can be done:

def __str__(self):
   return 'Value1={0}, Value2={1}'.format(self.value1, self.value2)

Upvotes: 1

Related Questions