Skip to content Skip to sidebar Skip to footer

Django __str__ Returned Non-string (type Nonetype)

I am getting __str__ returned non-string (type NoneType) error at edit Product Model object Product Model class Product(models.Model): ProductName = models.CharField(blank=T

Solution 1:

you can try:

def__str__(self):
    returnstr(self.ProductName) if self.ProductName else''

Solution 2:

I little late but change the return as follows:

def__str__(self):
    returnstr(self.ProductName)

Solution 3:

__str__ returned non-string (type NoneType)

is returned when the model you are accessing OR other models related to it is / are returning an int value on the def __str__ function. Should you be returning an int, be sure to parse the value to a String option.

def__str__(self):
    returnstr(self.id)

Solution 4:

The issue is on Foregienkey :

 RelatedStore = models.ForeignKey(Store, blank=True, null=True)

#edit or delete this:
    def __str__(self):
        returnself.ProductName

or else check the Store class there will be some

def_str_(self):
 returnself.something 

then it will work fine , I know i am late but that worked for me !!

Solution 5:

I found the solution, just make sure that the table (or the model) does not return and does not contain any attribute (or foreign key) whose the table / model returns an integer. You can use this for those attribute if you want to return primary key:

def__str__(self):
        return (str(self.pk))

In your case, make sure that the RelatedStore class / model / table returns a string.

Post a Comment for "Django __str__ Returned Non-string (type Nonetype)"