27👍
input_formats
needs to be a list, see
example:
['%Y-%m-%d', # '2006-10-25'
'%m/%d/%Y', # '10/25/2006'
'%m/%d/%y'] # '10/25/06'
https://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.DateField
14👍
I prefer to set **DATE_INPUT_FORMATS
in settings.py and then define the field like:
dob = forms.DateField(input_formats=settings.DATE_INPUT_FORMATS)
This more DRY than setting it on a per form field basis.
If it’s a text field input, I almost always put the accepted format(s) in the field’s help_text
so that the user can know what format(s) is(are) accepted.
- Use Django dumpdata to dump a subset of overall data?
- Can I slow down Django
- Django filter a ForeignKey field when it is null
- Sending a message to a single user using django-channels
- SerializerClass field on Serializer save from primary key
4👍
You do not need to add a new field once you already have a DateField
in your Model.
You just have to set the input_formats
for this field:
self.fields[ 'dob' ].input_formats = [ '%Y-%m-%d' ]
References: DateField and Format Localization
- In Django, how do I clear a sessionkey?
- UUID field added after data already in database. Is there any way to populate the UUID field for existing data?
- Django – Change a ForeignKey relation to OneToOne
0👍
The input_formats
is your friend.
If you accept javascript, then you could make the field use something like jQuery calendar (i.e. a read-only text field and clicking on it will call the jquery code and pop up the calendar widget). You can have this calendar widget as a starting point.
- TypeError: create_superuser() missing 1 required positional argument: 'profile_picture'
- DjangoRestFramework HTTPS Links With Routers and Viewsets
- Can't debug Django unit tests within Visual Studio Code
- Catch exception on save in Django admin?