How to Solve Django Error Message : TypeError: coerring to Unicode: need string or buffer, NoneType found

Posted on

This is an article where the article is focusing on how to solve Django error message. The error message which is solved actually an error message happened in a web-page application based on Django web-based framework. The error message is shown below :

TypeError: coercing to Unicode: need string or buffer, NoneType found

The error is actually caused by a field exist in a table where the field itself is actually have a NULL value. Since it is the default value and it is actually doesn’t have any value at all in this context, the error generated that the field has an error type because there is the NULL value so it cannot be associated into any kind of type which is concluded as a NoneType found.  Below is the actual type of the column as shown below :

mysql> desc mytable;
+------------------+--------------+------+-----+---------+----------------+
| Field            | Type         | Null | Key | Default | Extra          |
+------------------+--------------+------+-----+---------+----------------+
| id               | int(5)       | NO   | PRI | NULL    | auto_increment |
| ...              |              |      |     |         |                |
| name             | varchar(255) | YES  |     | NULL    |                |
| ...              |              |      |     |         |                |          
+------------------+--------------+------+-----+---------+----------------+
25 rows in set (0,01 sec)

mysql> 

The error is actually shown at the time the model which is represented by the table shown in the above display output is being rendered. The rendering process is represented by the following line in the Django script file based on python named ” :

from __future__ import unicode_literals

from django.db import models

# Create your models here.

class myclass(models.Model):
    name = models.CharField(max_length=255, null=True)

def __unicode__(self):
    return self.name

The above is actually is  a piece of code which is returning the value of the name attribute or property of the class named myclass. It is actually representing the field or column named ‘name’ from the table named ‘mytable’.  But since the field doesn’t have any value or it is by default a NULL value, when the page is loaded, the above error is triggered. So, in order to solve the problem, just fill the value or change the attribute or property name into something else which is represented by another column in the table where the column itself has a value inside of it. For instane, if there is another field name ’email’ and it is also defined in the class, just change it as follows :

from __future__ import unicode_literals

from django.db import models

# Create your models here.

class myclass(models.Model):
    name = models.CharField(max_length=255, null=True)
        email = models.CharField(max_length=255, default=user@mail.com)

def __unicode__(self):
    return self.email

Leave a Reply