仅包含月份和年份的Django DateField [英] Django DateField with only month and year

查看:572
本文介绍了仅包含月份和年份的Django DateField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之所以发布此信息,是因为找不到适合我的答案/示例,因此我以一种在那里没有看过的方式解决了它。

I post this because I could not find an answer/example that satisfied me and I solved it a way I haven't read in there. Maybe it can be useful or can be discussed.

基本上,我的应用程序显示用户可以在其中创建对象的表单集。验证后,将再次显示表单集,用户可以添加新对象。
指定一天是不相关的。

Basically, my app displays formsets where users can create objects. Once validated, the formset is displayed again and user can add new objects. Specifying a day is not relevant.

我首先开始使用一些javascript来做到这一点,然后使用Django自定义模型(将其归为models.DateField)。 。

I first started to use some javascript to do it, then played with Django custom model (to subclass models.DateField).

后来我无法成功做到这一点(我的帖子已与此链接链接
由于我有截止日期,因此我使用了简单的ChoiceField来将对象存储为Integers,然后,请使用@property方法从这些用户输入中构建一个正确的日期(每天添加1个)。

I could not succeed to do it with the later (my post updated linked to this one) As I have deadlines, I did it with simple ChoiceField storing objects as Integers and then, use a @property method to build a proper date out of those user inputs (add 1 as day).

这样,我避免深入研究Django蒸汽管道,甚至摆脱了日期选择器!

This way I avoided to deep dive in Django steam pipes and even got rid of a datepicker!

但是感谢@property,我仍然可以在Django模板中保留格式化日期和进行日期计算等便利。

But thanks to the @property, I can still keep the convenience of formatting dates in Django templates and make date calculation etc...

models.py:

models.py :

注意:如果无法从填充字段中建立日期,请返回无。否则,尝试在模板中呈现这些@property字段时,将出现未绑定错误。

class MyModel(models.Model):

    YEAR_CHOICES = [(y,y) for y in range(1968, datetime.date.today().year+1)]
    MONTH_CHOICE = [(m,m) for m in range(1,13)]

    start_year = models.IntegerField(choices=YEAR_CHOICES,
                 default=datetime.datetime.now().year,)
    start_month = models.IntegerField(choices=MONTH_CHOICE,
                  default=datetime.datetime.now().month,)
    end_year = models.IntegerField(choices=YEAR_CHOICES,
               default=datetime.datetime.now().year,)
    end_month = models.IntegerField(choices=MONTH_CHOICE,
                default=datetime.datetime.now().month,)

    @property
    def start_date(self):
        if self.start_year and self.start_month:
            return datetime.date(self.start_year, self.start_month, 1)
        elif self.start_year:
            return datetime.date(self.start_year, 12, 31)
        else:
            return None

    @property
    def end_date(self):
        if self.end_year and self.end_month:
            return datetime.date(self.end_year, self.end_month, 1)
        elif self.end_year:
            return datetime.date(self.end_year, 12, 31)
        else:
            return None

forms.py

forms.py

class MyModelForm(forms.ModelForm):
    class Meta:
        model = MyModel
        exclude = ("blabla",)

现在,如果要在表单模板中显示日期,则可以使用:

Now, if you want to display a date in a form template, you can use :

{{ form.instance.start_date }}

希望它会有所帮助&欢迎提出建议!

Hope it helps & Suggestions are welcome!

推荐答案

我认为您的开始日期结束日期函数需要更改:

I think your start_date and end_date function need to be changed:

def start_date(self):
    if self.start_year and self.start_month:
        return datetime.date(self.start_year, self.start_month, 1)
    elif self.start_year:
        # return Jan 1st
        return datetime.date(self.start_year, 1, 1)
    else:
        return None

def end_date(self):
    if self.end_year and self.end_month:
        if self.end_month == 12:
            # if month is December, return December 31st
            return datetime.date(self.end_year, self.end_month, 31)
        else:
            # else, get first day of next month and subtract 1 day.
            return datetime.date(self.end_year, self.end_month+1, 1) - datetime.timedelta(days=1)
    elif self.end_year:
        return datetime.date(self.end_year, 12, 31)
    else:
        return None

这篇关于仅包含月份和年份的Django DateField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆