Modify how a field displays on Django tables2 table

JoeV

I am trying to take a date field and display it in a django tables2 table. The issue is my field uses Djangos DateTimeField and has the time added on to the end which I don't care to display. My initial thought was to use a property decorator, and reference that in my table rather than using my original date field. But when I try to use split inside my property function nothing gets displayed in my table. If I modify the function to just print a string without using split it seems to work as intended. It seems that I can't use split anywhere in the function, even if I return a hard coded string instead of whatever I am splitting. Using split within the function just breaks it and makes it display nothing. Why can I not use split inside my function, and is there an alternative or better way of modifying what the date looks like when it displays on my 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"
crimsonpython24

You cannot split whatever is returned from the column because it is Python's datetime.datetime object. 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.

And according to Marco, it seems like to can change the format of the datetime object yourself. Documentation

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

In Django Tables2, how do you make a column show text from a table referenced by a foreign key?

Exporting of Django tables2

How to modify values in m2m field in form? (Django)

Dynamic tables with Django tables2

Getting Django Tables2 to work

Django Tables2 duplicated results

How to modify a boolean field model in Django?

How to modify cloned field form value and id inside table row?

Django tables2 add custom column for another model

Add link on a web page to export tables2 data in Django

Django modify bridge table

Django/Pythons is Messages - Unique Error displays the field

How to match field of different table in Django

How to access a field through a sub table Django

Modify field format in listview Django

How to join to 2 different tables based on field value of main table?

How do I need to modify this MERGE statement, for a mass update of one field based on the value of a field in another table?

How do I modify this truth table so that it uses and displays 1's and 0's rather than true and false

Is there a way to modify a field in a table line by line based upon the contents of each individual field (DB2 or other)?

How to modify the data in a table

how to modify user table

Is it right that django migrations are running the filter of django tables2 module before running the migrations?

How to copy an One2many field in an onchange method and modify it?

How to dynamically modify a form field based on 2 other fields in Symfony?

Django SQL: related tables, use latest() on field of parent table

How to modify field of a struct in a slice?

Modify field names in serializer in Django Rest Framework

django filter and order by not being able to modify field?

Django: modify model’s field before save