2016-05-13 57 views
-1

關於這個主題的章節沒有提供如何去做的例子。我希望有人能夠根據我提供的內容推斷它,或者可能很幸運,並且有人閱讀了可以提供幫助的書。Python練習碰撞課程:練習18-2

這裏是exerpt:練習18-2。短條目:Entry模型中的__str__()方法當前爲當Django在管理站點或shell中顯示它時Entry的每個實例附加一個省略號。 if聲明到__str__()方法,僅當條目長度超過50個字符時才添加省略號。使用管理站點添加長度小於50個字符的條目,並檢查查看時是否沒有省略號。 「

的代碼塊是在底部:

from django.db import models 

class Entry(models.Model): 
    """Something specific learned about a topic.""" 
    topic = models.ForeignKey(Topic) 
    text = models.TextField() 
    date_added = models.DateTimeField(auto_now_add=True) 

    class Meta: 
     verbose_name_plural = 'entries' 

    def __str__(self): 
     """Return a string representation of the model.""" 
     return self.text[:50] + "..." 

回答

0

改變的__str__()的定義很簡單:

from django.db import models 

class Entry(models.Model): 
    """Something specific learned about a topic.""" 
    topic = models.ForeignKey(Topic) 
    text = models.TextField() 
    date_added = models.DateTimeField(auto_now_add=True) 

    class Meta: 
     verbose_name_plural = 'entries' 

    def __str__(self): 
     """Return a string representation of the model.""" 
     return self.text[:50] + ("..." if len(self.text) > 50 else "") 
+0

它的工作! Muchas gracias。 – fasilent