How to accept localized date format (e.g dd/mm/yy) in a DateField on an admin form?

后端 未结 2 1913
深忆病人
深忆病人 2021-01-06 04:22

Is it possible to customize a django application to have accept localized date format (e.g dd/mm/yy) in a DateField on an admin form ?

I have a model class :

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-06 04:49

    The admin system uses a default ModelForm for editing the objects. You'll need to provide a custom form so that you can begin overriding field behaviour.

    Inside your modelform, override the field using a DateField, and use the input_formats option.

    MY_DATE_FORMATS = ['%d/%m/%Y',]
    
    class MyModelForm(forms.ModelForm):
        date = forms.DateField(input_formats=MY_DATE_FORMATS)
        class Meta:
            model = MyModel
    
    class MyModelAdmin(admin.ModelAdmin):
        form = MyModelForm
    

提交回复
热议问题