2016-06-23 7 views
1

Der Versuch, ein Bild über das Django-Admin-Panel hochzuladen, und ich erhalte einen Fehler. Das Modell ist:Fehler beim Hochladen eines Image zu ImageField über Django Admin

fs = FileSystemStorage(location='/media') 

class Picture(models.Model): 
    picture_path = models.ImageField(upload_to=fs, blank=True) 
    hunter_ID = models.ForeignKey(User, 
            on_delete=models.CASCADE,) 
    bounty_ID = models.ForeignKey('bountyhunt.Bounty', 
            on_delete=models.CASCADE,) 
    winner = models.BooleanField(default=False) 
    example_pic = models.BooleanField(default=False) 

    def __str__(self): 
     return self.picture_path 

und der Fehler Ich erhalte ist:

TypeError at /admin/hunt/picture/add/ 
__str__ returned non-string (type ImageFieldFile) 
Request Method: POST 
Request URL: http://127.0.0.1:8000/admin/hunt/picture/add/ 
Django Version: 1.9.6 
Exception Type: TypeError 
Exception Value:  
__str__ returned non-string (type ImageFieldFile) 
Exception Location: /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/utils/encoding.py in force_text, line 76 
Python Executable: /Library/Frameworks/Python.framework/Versions/3.5/bin/python3 
Python Version: 3.5.1 
Python Path:  
['/Users/michael/Documents/phoboh', 
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python35.zip', 
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5', 
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin', 
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload', 
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages'] 

Irgendwelche Ideen?

Antwort

4

Wie der Fehler angibt, ist die Problem-ID mit __str__ Ihres Modells Picture. Anstatt einen String zurückzugeben, gibt es ein ImageFieldFile Objekt zurück.

ändern sie einen String zurück - zum Beispiel

def __str__(self): 
    return str(self.picture_path) 

oder:

def __str__(self): 
    return self.picture_path.name 
Verwandte Themen