-7
我在閱讀並試圖理解django文檔,所以我有一個合乎邏輯的問題。在Django中做什麼__str__函數?
還有就是我models.py
文件
from django.db import models
# Create your models here.
class Blog(models.Model):
name = models.CharField(max_length=255)
tagline = models.TextField()
def __str__(self):
return self.name
class Author(models.Model):
name = models.CharField(max_length=255)
email = models.EmailField()
def __str__(self):
return self.name
class Post(models.Model):
blog = models.ForeignKey(Blog)
headline = models.CharField(max_length=255)
body_text = models.TextField()
pub_date = models.DateField()
mod_date = models.DateField()
authors = models.ManyToManyField(Author)
n_comments = models.IntegerField()
n_pingbacks = models.IntegerField()
rating = models.IntegerField()
def __str__(self):
return self.headline
這到底是怎麼做的每一個__str__
功能每班? 我需要這些功能的原因是什麼?
Django的對象將使用該函數的結果顯示在管理界面該類型例如對象的多個人類友好名稱。 –
我不知道django,但你可以閱讀['__str__'](https://stackoverflow.com/questions/3691101/what-is-the-purpose-of-str-and-repr-in-python) – Rahul
閱讀文檔:https://docs.djangoproject.com/en/1.11/ref/models/instances/#str – jonrsharpe