1👍
✅
The __str__
only returns the self.first_name
hence it means that it will print the Person
as <Person: last_name>
with last_name
the last_name
of the Person
.
If you thus rewrite this to:
from django.db import models
class Person(models.Model):
first_name = models.CharField(max_length=15)
last_name = models.CharField(max_length=6)
def __str__(self):
return f'{self.first_name} {self.last_name}'
it will print the person object with its first and last name.
But saving it in the database, and retrieving it works, regardless of the implementation of __str__
.
If you for example obtain the .last_name
attribute, it will print:
>>> Person.objects.get(id=some_id).last_name
'...'
Source:stackexchange.com