Python _ str _ (self) and _ unicode _ (self) ,__ str ____ unicode __
Official documents: https://docs.python.org/2.7/reference/datamodel.html? Highlight =__ mro __
Object. _ Str __ (
Self )
Called byStr ()Built-in function and byPrintStatement to compute the "informal" string representation of an object. This differs from_ Repr __()In that it does not have to be a valid Python expression: a more convenient or concise representation may be used instead. The return value must be a string object.
[Translation] It is called through the embedded method str (), and the "informal" String Representation of the object is calculated using the print statement. This differs from _ repr _ () in that it does not need to be a legal Python expression: it can be used in a more convenient or concise way. The return type must be a string object.
Object. _ Unicode __ (
Self )
Called to implementUnicode ()Built-in; shocould return a Unicode object. When this method is not defined, string conversion is attempted, and the result of string conversion is converted to Unicode using the system default encoding.
Unicode () embedded functions are implemented. Unicode objects should be returned. If this method is not defined, it is replaced by string conversion. The conversion result is converted to Unicode using the system default encoding.
=============== The following content is translated from here ========================
_ Str _ () is a "magic" method of Python. This method defines the value that should be returned when the object calls str. Django uses str (obj) (or related methods, unicode (obj) in many places-see below ), for example, when the Django management site loads an object, it displays its value or inserts it into the template as the display value of the object. Therefore, we should always return a friendly, user-readable string as the object's _ str __. Although this is not necessary, it is recommended to do so. For example:
class Person(models.Model): first_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50) def __str__(self): # Note use of django.utils.encoding.smart_str() here because # first_name and last_name will be unicode strings. return smart_str('%s %s' % (self.first_name, self.last_name)
_ Unicode __
The _ unicode _ () method is called when unicode () is called on an object. Because the backend of the Django database returns the Unicode string to the model attribute, we usually write a _ unicode _ () method for our model. The preceding example can also be written as follows:
class Person(models.Model): first_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50) def __unicode__(self): return u'%s %s' % (self.first_name, self.last_name)
If the _ unicode _ () method is defined but the _ str _ () method is not defined, Django automatically provides the _ str _ () method __() the method calls the _ unicode _ () method and converts the result to a string object encoded by the UTF-8. In actual development, we recommend that you only define the _ unicode _ () method. If necessary, let Django handle the conversion of string objects.
============================================================
In Flask, the corresponding method for defining the data model of an Article class can be written as follows:
class Article(db.Document): Title = db.StringField(max_length=255, required=True) SegTitle = db.StringField(max_length=255) Url = db.StringField(max_length=255, required=True) Id = db.StringField(max_length=255, required=True) Summary = db.StringField(max_length=255) Content = db.StringField() SegContent = db.StringField() Tags = db.ListField(db.EmbeddedDocumentField(Tag)) StrTags = db.ListField(db.StringField(max_length=30)) LabeledTags = db.ListField(db.StringField(max_length=30)) CrawledDate = db.DateTimeField() PostDate = db.StringField() Source = db.StringField() OriginalSource = db.StringField() @property def post_type(self): return self.__class__.__name__ def __unicode__(self): return self.Title meta = { 'allow_inheritance': False }