__str__ returned non-string (type tuple)

后端 未结 2 1396
旧时难觅i
旧时难觅i 2021-01-02 15:00

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 coupl

2条回答
  •  灰色年华
    2021-01-02 15:42

    The error is in your model:

    class SourceOfInjuryLevel2(models.Model):
    
        ...
    
        def __str__(self):
            return self.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)
    

提交回复
热议问题