Vijay
Vijay

Reputation: 93

Django-models Change format of dateField as "DD-MM-YYY"

I am using django-model's-dateField in my django-project and wanna change my dateFields format in admin view.

Currently (default) django gives format in YYYY-MM-DD but i want to modify that and in need of DD-MM-YYYY

I've been struggled too much in this and till no solution.

Also providing my code, may help..

my settings.py have:

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

models.py:

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='User_Profile')
    mobile = models.BigIntegerField()
    alternative_number = models.BigIntegerField()
    DOB = models.DateField()

and then i register it in admin

output on admin

as you can see in my admin page there is a dateField with format 2019-11-04 but i want in form of DD-MM-YYYY as 04-11-2019.

I tried LANGUAGE_CODE = 'de-at' that would work for me but the problem is, it changes the local language also.

refering a similer post: Django - change default presentation format of model date field

Upvotes: 3

Views: 7381

Answers (3)

Alvaro Rodriguez Scelza
Alvaro Rodriguez Scelza

Reputation: 4174

fahim kazi was right: to define a models.DateField format, you need DATE_FORMAT, not DATETIME_FORMAT.

Yet Naveen Varshney was also right: not DATE_FORMAT nor DATETIME_FORMAT work without specifying the language, with es_formats for instance.

Therefore, from the combination of both answers, put the following in settings:

es_formats.DATE_FORMAT = 'd-m-y'

(Also '%d-%m-%y' printed %01-%08-%20 thus the % is not needed).

Thanks both.

Upvotes: 1

Naveen Varshney
Naveen Varshney

Reputation: 21

You need just to override Django’s defaults locale settings. Add this to your settings.py file

from django.conf.locale.es import formats as es_formats

es_formats.DATETIME_FORMAT = "d M Y H:i:s"

Upvotes: 1

fahim kazi
fahim kazi

Reputation: 106

add this to your settings.py

DATE_FORMAT = '%d-%m-%y'

for more check this date format

Upvotes: 1

Related Questions