修改字段在 Django tables2 table 上的显示方式

Modify how a field displays on Django tables2 table

我正在尝试获取日期字段并将其显示在 django tables2 table 中。问题是我的字段使用 Djangos DateTimeField 并将时间添加到我不想显示的末尾。我最初的想法是使用 属性 装饰器,并在我的 table 中引用它,而不是使用我原来的日期字段。但是当我尝试在我的 属性 函数中使用 split 时,我的 table 中没有显示任何内容。如果我将函数修改为只打印一个字符串而不使用拆分,它似乎可以按预期工作。似乎我不能在函数的任何地方使用 split,即使我 return 一个硬编码字符串而不是我正在拆分的任何内容。在函数内使用 split 只会破坏它并使其不显示任何内容。为什么我不能在我的函数中使用 split,有没有其他或更好的方法来修改显示在我的 table 上的日期?

#tables.py
class ReportTable(tables.Table):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    class Meta:
        attrs = {"class": "mytable"}
        model = models.Report
        fields = ("formatted_date")
#models.py
class Report(models.Model):
    id = models.AutoField(db_column="RootCauseID", primary_key=True)
    date = models.DateTimeField(db_column="date", blank=False, null=True)

    @property
    def formatted_date(self):
        date = self.date
        date_list = date.split()
        final_date = date_list[0]
        return f"{final_date}"

    class Meta:
        managed = True
        db_table = "Report"

您不能拆分从列返回的任何内容,因为它是 Python 的 datetime.datetime 对象。 Documentation

class DateTimeField(auto_now=False, auto_now_add=False, **options)

A date and time, represented in Python by a datetime.datetime instance. Takes the same extra arguments as DateField.

根据 Marco 的说法,似乎可以自己更改日期时间对象的格式。 Documentation

tables.DateTimeColumn(format ='M d Y, h:i A')