timedelta days组件的不支持类型:NoneType

时间:2016-04-30 08:56:50

标签: python django

我是python / django的新手,我已经遇到了上述错误。我看过一些帖子,但似乎没有任何帮助。在我的应用程序中,用户可以从三个选项中选择提醒,如果他选择第三个选项“选择日期”,他会在提醒字段中键入天数。 所以,这是我的模型:

class Event(models.Model):

    # some other fields

    reminder=models.IntegerField('Reminder', default=0, choices=REMINDER_STATUS)
    reminderx = models.IntegerField('Days',blank=True, null=True)

这是我视野中的代码:

    today = date.today()
    a = Event.objects.filter(del_f=0)       
    is_today = []
    is_week=[]
    is_custom =[]

    for i in a:
        print('i:',i)
        reminder = i.reminder

        if reminder == 1 :
            rtoday = i.start_date.date() - timedelta(days=1)
            print('rtoday:',rtoday)
            if rtoday == today:
                is_today.append(i)  
        if reminder == 2 :
            rweek = i.start_date.date() - timedelta(days=7)
            if rweek == today:
                is_week.append(i)            

        if reminder == 3 :
            reminderx = i.reminderx
            rcustom = i.start_date.date() - datetime.timedelta(days= reminderx)
            if rcustom == today:
                is_custom.append(i)

    context['today'] = is_today
    context['week'] = is_week
    context['custom'] = is_custom

    return context

提前感谢您的帮助!!

1 个答案:

答案 0 :(得分:1)

错误明确指出days的{​​{1}}参数为timedelta()(它需要None)。在您的代码中,您只有一行代表int使用timedelta()的变量:

days

所以这意味着变量rcustom = i.start_date.date() - datetime.timedelta(days= reminderx) reminderx。如果查看模型定义,None可以为null。您需要使模型不接受空值或在使用属性时具有一些默认值:

reminderx