在django Modelform中以所需格式显示日期

Displaying date in a required format in django Modelform

我有以下内容:

型号:

class customer(models.Model):
    cstid = models.AutoField(primary_key=True, unique=True)
    name = models.CharField(max_length=35)
    ageyrs=models.IntegerField(blank=True)
    agemnths=models.IntegerField(blank=True)
    dob = models.DateField(null=True, blank=True)
    gender_choices = (('male', 'Male'),
        ('female', 'Female'),
        ('other', 'Something else'),
        ('decline', 'Decline to answer'))
    gender = models.CharField(
        choices=gender_choices, max_length=10, default='male')
    maritalstatus_choices = (('unmarried', 'Unmarried'),
        ('married', 'Married'))
    maritalstatus = models.CharField(
        choices=maritalstatus_choices, max_length=10, default='Unmarried')
    mobile = models.CharField(max_length=15, default='')
    alternate = models.CharField(max_length=15, default='', blank=True)
    email = models.CharField(max_length=50, default='', blank=True)
    address = models.CharField(max_length=80, default='', blank=True)
    city = models.CharField(max_length=25, default='', blank=True)
    occupation = models.CharField(max_length=25, default='', blank=True)
    bloodgroup_choices = (('apos', 'A+'),
        ('aneg', 'A-'),
        ('bpos', 'B+'),
        ('bneg', 'B-'),
        ('opos', 'O+'),
        ('oneg', 'O-'),
        ('abpos', 'AB+'),
        ('abneg', 'AB-')
        )
    bloodgroup = models.CharField(choices=bloodgroup_choices, max_length=5, default='-', blank=True)

    class Meta:
        unique_together = ["name", "mobile", "linkedclinic"]

我的模型:

class RegisterPatientMetaForm(ModelForm):
    class Meta:
        dob = forms.DateField(input_formats=['%d-%m-%Y'])
        model = customer
        fields = [
            'name',
            'ageyrs',
            'agemnths',
            'dob',
            'gender',
            'maritalstatus',
            'mobile',
            'alternate',
            'email',
            'address',
            'city',
            'occupation',
            'bloodgroup'
            ]

在我的模板中,我有:

<div class="col-md-8">
    <label for="gender">Date of Birth</label>
    {{ form.dob }}
</div>

问题是日期显示为 %Y-%m-%d,而我希望它显示为 %d-%m-%Y。我这样做有什么问题?我该如何解决这个问题?

当您覆盖表单的一个字段时,您需要将其作为 class 的属性,而不是放在元 class 中。像这样:

class RegisterPatientMetaForm(ModelForm):
    dob = forms.DateField(input_formats=['%d-%m-%Y'])  # <-- removed it from meta and put it here
    class Meta:
        model = customer
        fields = [
            'name',
            'ageyrs',
            'agemnths',
            'dob',
            'gender',
            'maritalstatus',
            'mobile',
            'alternate',
            'email',
            'address',
            'city',
            'occupation',
            'bloodgroup'
            ]

@ruddra 的回答只对了一部分。 我的问题有两个不同的方面。一方面,我需要以所选日期格式显示现有数据库行。为此,我需要自定义 forms.DateInput 小部件,以便正确显示现有值。其次,我也需要接受所选格式的输入。

所以代码的解决方案是:

class RegisterPatientMetaForm(ModelForm):
    dob = forms.DateField(
        input_formats=['%d-%m-%y'],
        widget=forms.DateInput(format='%d-%m-%y')
        )
    class Meta:        
        model = customer
        fields = [
            'name',
            'ageyrs',
            'agemnths',
            'dob',
            'gender',
            'maritalstatus',
            'mobile',
            'alternate',
            'email',
            'address',
            'city',
            'occupation',
            'bloodgroup'
            ]
        error_messages = {           
        }
        unique_together = ["name", "mobile", "linkedclinic"]

此处,input_formats=['%d-%m-%y'] 列表决定接受哪些日期格式作为输入(参见 Docs). While the widget=forms.DateInput(format='%d-%m-%y') makes the initial field displayed correctly (See Docs