Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display only date from datetime field in modelAdmin in Django admin panel?

I have a model which has a column created_at which is actually a datetime field of this format 16.07.2019 12:26:37.

I want to display only date present in this created_at field in admin panel in Django.

I can display the whole field's value which is datetime if I mention the 'created_at' in list_display while creating modelAdmin.

How can I display only the date value in results page like this 16.07.2019?

Are there any inbuilt filters to do that while registering the modelAdmin?

Python: 3.7.3
Django: 2.1.5
like image 715
Underoos Avatar asked Jan 18 '26 13:01

Underoos


1 Answers

You can add a method to the ModelAdmin class.

@admin.register(YourModel)
class YourModelAdmin(admin.ModelAdmin):
    list_display = ('get_date_formatted',)

    def get_date_formatted(self, obj):
        if obj:
            return obj.date.date()
    get_date_formatted.admin_order_field = 'date'
    get_date_formatted.short_description = 'date'
like image 96
bdoubleu Avatar answered Jan 21 '26 01:01

bdoubleu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!