donkeyboy72
donkeyboy72

Reputation: 1943

Django 'coercing to Unicode: need string or buffer, NoneType found

I'm working on a simple app and I just added a new module called Picture to my models.py . When I try to access the Person module at the admin page , I receive this error . I already did delete my database and syncdb

When I click on other module such as Picture and Pet , It was successful but when I clicked on Person at admin page I receive this error

I did some research and one solution was to return a valid unicode . I think , I done it

TypeError at /admin/pet/person/

coercing to Unicode: need string or buffer, NoneType found
Request Method:     GET
Django Version:     1.4.3
Exception Type:     TypeError
Exception Value:    

coercing to Unicode: need string or buffer, NoneType found

In template C:\Python26\lib\site-packages\django\contrib\admin\templates\admin\change_list.html, error at line 93
{% result_list cl %}

The problem is relating to my modules.py

from django.db import models
from django.db.models.signals import post_save
from django.contrib.auth.models import User

class Person(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=100,blank=True,null=True)

    def __unicode__(self):
        return self.name

class Picture(models.Model):
    user = models.ForeignKey(Person)
    image = models.FileField(upload_to="images/",blank=True,null=True)
    name = models.CharField(max_length=100,blank=True,null=True)

    def __unicode__(self):
        return self.name


class Pet(models.Model):
    Person = models.ForeignKey(Person)
    description = models.CharField(max_length=100)
    image = models.FileField(upload_to="images/",blank=True,null=True)

    def __unicode__(self):
        return self.description

My admin.py

 from django.contrib import admin
 from pet.models import *

 admin.site.register(Person)
 admin.site.register(Pet)
 admin.site.register(Picture)

Upvotes: 1

Views: 10050

Answers (1)

okm
okm

Reputation: 23871

Check whether there is any Person object having name of None value.
Also better to remove null=True configuration from the name field of the Person model. It's not a good practice to have CharField being nullable.

>>> class Foo(object):
...     name = None
...     def __unicode__(self):
...         return self.name
>>> unicode(Foo())
TypeError: coercing to Unicode: need string or buffer, NoneType found

Upvotes: 9

Related Questions