Model.__str__()
[source]
The __str__()
method is called whenever you call str()
on an object. Django uses str(obj)
in a number of places. Most notably, to display an object in the Django admin site and as the value inserted into a template when it displays an object. Thus, you should always return a nice, human-readable representation of the model from the __str__()
method.
For example:
1 2 3 4 5 6 7 8 9 10 | from django.db import models from django.utils.encoding import python_2_unicode_compatible @python_2_unicode_compatible # only if you need to support Python 2 class Person(models.Model): first_name = models.CharField(max_length = 50 ) last_name = models.CharField(max_length = 50 ) def __str__( self ): return '%s %s' % ( self .first_name, self .last_name) |
If you’d like compatibility with Python 2, you can decorate your model class with python_2_unicode_compatible()
as shown above.
Please login to continue.