首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在django Modelform中以所需格式显示日期

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

Stack Overflow用户
提问于 2019-03-14 01:38:00
回答 2查看 110关注 0票数 0

我有以下几点:

型号:

代码语言:javascript
运行
复制
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"]

我的ModelForm:

代码语言:javascript
运行
复制
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'
            ]

在我的模板中,我有:

代码语言:javascript
运行
复制
<div class="col-md-8">
    <label for="gender">Date of Birth</label>
    {{ form.dob }}
</div>

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-14 07:57:09

@ruddra的答案只有部分正确。我的问题有两个不同的方面。一方面,我需要以选定的日期格式显示现有的数据库行。为此,我需要定制forms.DateInput小部件,以便正确显示现有值。在第二种情况下,我也需要接受所选格式的输入。

因此,代码的解决方案是:

代码语言:javascript
运行
复制
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)

票数 1
EN

Stack Overflow用户

发布于 2019-03-14 01:45:39

当覆盖表单的字段时,需要将其作为类的属性,而不是放在元类中。如下所示:

代码语言:javascript
运行
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55148113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档