Let's say I have a model
class Job(Model):
created_on = DateTimeField(_("Created On"), auto_now=True, editable=False)
name = CharField(_("Name"), max_length=300, blank=False)
Now I am trying to aggregate all fields with their verbose name and value to display it in a template.
My aggregating function:
def get_fields(self):
result = []
for field in Job._meta.fields:
result.append((field.name, field.verbose_name, field.value_to_string(self)))
This gives me the values as string properly, however for a datetime field, I end up with a dumb unformatted string like 2021-02-13T22:39:47.561045+00:00 I really don't want to check and modify the value in some way by specifically checking for the particular field as that is the whole point of my get_field function
I feel any of the following should work fine for me:
- Somehow provide the default format to use when calling value_to_string()
- Get a proper dateTime object instead, in which case the value should show up fine on the template
- Get field type and then instead of calling value_to_string() I manually convert the string to this type (This seems feasible from what I see but feels too hacky of a solution)