Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python速成课程中的练习:练习18-2_Python_Django Admin - Fatal编程技术网

Python速成课程中的练习:练习18-2

Python速成课程中的练习:练习18-2,python,django-admin,Python,Django Admin,关于这个主题的一章没有提供一个如何做的例子。我希望有人能根据我提供的东西推断出来,或者可能会幸运地发现,有人读过这本书,谁能帮上忙 下面是练习:“练习18-2.简短条目:当Django在管理站点或shell中显示条目实例时,条目模型中的\uuu str\uuuu()方法当前会在每个条目实例中添加一个省略号。在\uu str\uuu()中添加一个if语句仅当条目长度超过50个字符时才添加省略号的方法。使用管理网站添加长度小于50个字符的条目,并检查查看时是否没有省略号。” 代码块位于底部: fro

关于这个主题的一章没有提供一个如何做的例子。我希望有人能根据我提供的东西推断出来,或者可能会幸运地发现,有人读过这本书,谁能帮上忙

下面是练习:“练习18-2.简短条目:当Django在管理站点或shell中显示条目实例时,条目模型中的
\uuu str\uuuu
()方法当前会在每个条目实例中添加一个省略号。在
\uu str\uuu()中添加一个
if
语句
仅当条目长度超过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] + "..."

更改
\uuu str\uuu()
的定义很简单:

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 "")
from django.db import models

class Entry(models.Model):
    """Something specific learned about a topic."""
    topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
    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."""
        if len(self.text) > 50:
            return f"{self.text[:50]}..."
        else:
            return f"{self.text}"