如何以给定格式打印数据

时间:2013-08-15 00:56:51

标签: python django django-models django-forms django-templates

models.py

class ReportPerson(models.Model):
    report = models.ForeignKey(Report)
    name = models.CharField('Name', max_length=100)

views.py

def method(request):
    involver_namelist = [] 
    involved = ReportPerson.objects.filter(report=report)
    for involve in involved:
        involver_name =involve.name  
        involver_namelist.append(involver_name)
        print involver_namelist
    return render

如果我打印来自involver_namelist的数据,它打印如[u'Admin', u'Admin', u'Admin', u'Admin', u'Admin'],但我希望输出像Admin, Admin, Admin, Admin, Admin。这是可能的django。

2 个答案:

答案 0 :(得分:0)

当您运行print involver_namelist时,它正在打印list

要获得所需的输出,请更换该行 -

print ", ".join(involver_namelist)

这将为您提供所需的Admin, Admin, Admin, Admin, Admin输出。

Using the join function for strings.

答案 1 :(得分:0)

我认为您应该在模型类下定义 repr 方法 在你的情况下,它会是这样的:

class ReportPerson(models.Model):
    ....
    def __repr__(self):
        return self.name

希望这会有所帮助