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

问题描述 投票:0回答:2

我有以下内容:

模型:

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。我怎么做的有什么问题?我怎样才能解决这个问题?

django modelform
2个回答
1
投票

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

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'
            ]

1
投票

@ 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)。而widget=forms.DateInput(format='%d-%m-%y')使初始字段正确显示(见Docs

© www.soinside.com 2019 - 2024. All rights reserved.