European date input in Django Admin

前端 未结 6 1267
-上瘾入骨i
-上瘾入骨i 2021-01-02 13:30

Django has a DATE_FORMAT and a DATE_TIME_FORMAT options that allow us to choose which format to use when viewing dates, but doesn\'t apparently let me change the input forma

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-02 13:40

    You have to do this yourself for now, but it's quite easy to do with a custom form field class that sets the input_formats argument of DateField. This should do it:

    class MyDateField(forms.DateField):
      def __init__(self, *args, **kwargs):
        kwargs.setdefault('input_formats', ("%d-%m-%Y",))
        super(MyDateField, self).__init__(*args, **kwargs)
    

    Note that input_formats is a list, so you can specify multiple possibilities and it will try them in order when parsing user input.

提交回复
热议问题