I have a Model with DateField
. In Django admin when I try to enter the date in Django Admin in format %d-%m-%Y
it throws error as Enter a valid date.
. Since the default pattern is %Y-%m-%d
I tried the following but couldnt find a satisfied solution over the docs:
- Change the format type with strftime in model.
- Add
DATE_INPUT_FORMATS
in settings. - Change language code to "en-IN"
Is there any way to save the date in %d-%m-%Y
format.
CodePudding user response:
class YourModel(models.Model):
date = models.DateField('Date', null=True, blank=True)
Insert this in Settings.py
DATE_FORMAT = ( ( 'd-m-Y' ))
DATE_INPUT_FORMATS = ( ('%d-%m-%Y'),)
DATETIME_FORMAT = (( 'd-m-Y H:i' ))
DATETIME_INPUT_FORMATS = (('%d-%m-%Y %H:%i'),)
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Asia/Kolkata'
USE_I18N = True
USE_L10N = False
USE_TZ = False
CodePudding user response:
You have to go into your model and add a couple of line:
import datetime
class yourModel(models.model):
date = field etc
def __str__(self):
return date.strftime('%d-%m-%Y')