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

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

I have a model class:

class MyModel(models.Model):    
    date  = models.DateField("Date")    

And the related admin class

class MyModelAdmin(admin.ModelAdmin):
     pass

In the django administration interface, I would like to be able to enter a date in the following format: dd / mm / yyyy. However, the date field in the admin form expects yyyy-mm-dd.

How can I customize things? Nota bene: I already specified my language code (fr-FR) in settings.py, but it does not seem to affect this date entry issue.

Thank you in advance for your reply.

+3
source share
2 answers

ModelForm . , .

DateField input_formats.

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
+4

, , , .

. % d %% M/% Y.

from django.forms.models import ModelForm
from django.contrib.admin.widgets import AdminDateWidget
from django.forms.fields import DateField  

class MyModelForm(ModelForm):
    date = DateField(input_formats=['%d/%m/%Y',],widget=AdminDateWidget(format='%d/%m/%Y'))
    class Meta:
        model = MyModel

class MyModelAdmin(admin.ModelAdmin):
    form = MyModelForm

% Y-% m-% d, .

PS:

+2

Source: https://habr.com/ru/post/1736822/


All Articles