Skip to content Skip to sidebar Skip to footer

__str__ Returned Non-string (type Tuple)

I have a form that keeps throwing me an error in django, Ive tried searching online tried str() on my models but wouldnt work at all. Googled a couple of times tried a couple diffe

Solution 1:

The error is in your model:

classSourceOfInjuryLevel2(models.Model):

    ...

    def__str__(self):
        returnself.soi_l1, self.soi_l2, self.status

I guess you were confused because the Python 2 print statement looks like it turns tuples into strings, but that's not actually how the print statement works - it's a confusing detail that was changed in Python 3.

Try this instead:

def__str__(self):
    template = '{0.soi_l1} {0.soi_l2} {0.status}'return template.format(self)

Solution 2:

Those commas aren't actually doing what you think they do. The commas make your return value a tuple instead of a string which a __str__ method is supposed to return.

You can instead do:

def __str__(self):
    return'%s %s %s'%(self.soi_l1, self.soi_l2, self.status)

Or use the new-style formatting:

def __str__(self):
    return'{} {} {}'.format(self.soi_l1, self.soi_l2, self.status)

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