如何修改django admin中的__str__方法分两行打印
How to modified the __str__ method in django admin to print in two lines
型号
class Linea_Invest(models.Model):
nombre = models.CharField(max_length=50, blank=False, null=True, unique= True)
carrera = models.ForeignKey(
Carrera, on_delete=models.CASCADE, blank=False, null=True)
class Meta:
constraints = [
models.UniqueConstraint(fields=['nombre', 'carrera'], name='no repetir nombre carrera linea')
]
def __str__(self):
return f"Linea de investigación:{self.nombre}\nCarrera: {self.carrera}"
这在控制台中有效,但在我的模板和管理员中显示时,我得到如下信息:
期望的输出
研究方向:软件工程
专业:计算机工程
使用format_html(...)
函数
from django.utils.html import format_html
class Linea_Invest(models.Model):
def __str__(self):
<b>html_content = "First Line <br>" \
"Second Line <br>" \
"Third Line"
return format_html(html_content)</b>
型号
class Linea_Invest(models.Model):
nombre = models.CharField(max_length=50, blank=False, null=True, unique= True)
carrera = models.ForeignKey(
Carrera, on_delete=models.CASCADE, blank=False, null=True)
class Meta:
constraints = [
models.UniqueConstraint(fields=['nombre', 'carrera'], name='no repetir nombre carrera linea')
]
def __str__(self):
return f"Linea de investigación:{self.nombre}\nCarrera: {self.carrera}"
这在控制台中有效,但在我的模板和管理员中显示时,我得到如下信息:
期望的输出
研究方向:软件工程
专业:计算机工程
使用format_html(...)
函数
from django.utils.html import format_html
class Linea_Invest(models.Model):
def __str__(self):
<b>html_content = "First Line <br>" \
"Second Line <br>" \
"Third Line"
return format_html(html_content)</b>