[Django]-Return self.user as foreignkey

5👍

As your error show:

TypeError __str__ returned non-string (type User)

The __str__ method should return a str, and here, you are returning an object (User).

You should return return self.user.__str__ instead (The str representation of the user).

7👍

You need to refer to the specific field within ‘User’ you want to display.
Try:

def __str__(self):
    return self.user.username

2👍

you can only return string from __str__ not user which is an object.

def __str__(self):
    return self.user.username
👤navyad

2👍

You need to show the attribute of the user that you wish to display

def __str__(self):
   return self.user

should return any of the following (or similar)

return str(self.user)
return self.user.get_username()
return self.user.get_full_name()
👤Sayse

Leave a comment