减去models.DateField以获取天数

时间:2013-01-11 21:02:01

标签: django django-models django-views

我有一个跟踪工作假请求的简单模型:

class LeaveRequest(models.Model):
    employee = models.ForeignKey(UserProfile)
    supervisor = models.ForeignKey(UserProfile, related_name='+', blank=False, null=False)
    submit_date = models.DateField(("Date"), default=datetime.date.today)
    leave_type = models.CharField(max_length=64, choices=TYPE_CHOICES)
    start_date = models.DateField(("Date"))
    return_date = models.DateField(("Date"))
    total_days = models.IntegerField()
    notes = models.TextField(max_length=1000)

    def __unicode__ (self):
        return u'%s %s' % (self.employee, self.submit_date)

        class Admin: 
                pass

        class Meta:
                ordering = ['-submit_date']

在视图中我需要一个函数来计算请求的天数。其次,我需要一种方法来计算工作日,但现在我有以下几点:

def leave_screen(request, id):
    records = LeaveRequest.objects.filter(employee=id)
    total_days = LeaveRequest.return_date - LeaveRequest.start_date
    tpl = 'vacation/leave_request.html'
    return render_to_response(tpl, {'records': records })

产生属性错误

type object 'LeaveRequest' has no attribute 'return_date

有什么建议吗?

3 个答案:

答案 0 :(得分:1)

在total_days中,您正在调用模型而不是该模型的实例 - records - 您创建的。

如果您只想查看一个Leave记录,则需要传递LeaveRequest

的ID
def leave_screen(request, id):
    records = LeaveRequest.objects.get(id=id)
    total_days = records.return_date - records.start_date
    tpl = 'vacation/leave_request.html'
    return render_to_response(tpl, {'records': records })

答案 1 :(得分:1)

建议将其用作属性的答案可行但我认为我更愿意将其保留为字段并在插入时计算它。

class LeaveRequest(models.Model):
    employee = models.ForeignKey(UserProfile)
    supervisor = models.ForeignKey(UserProfile, related_name='+', blank=False, null=False)
    submit_date = models.DateField(("Date"), default=datetime.date.today)
    leave_type = models.CharField(max_length=64, choices=TYPE_CHOICES)
    start_date = models.DateField(("Date"))
    return_date = models.DateField(("Date"))
    total_days = models.IntegerField()
    notes = models.TextField(max_length=1000)

    def __unicode__ (self):
        return u'%s %s' % (self.employee, self.submit_date)

    def save(self, *args, **kwargs):
        self.total_days = (self.return_date - self.start_date).days
        super(LeaveRequest, self).save(*args, **kwargs)

        class Admin: 
                pass

        class Meta:
                ordering = ['-submit_date']

这样,当您输入排除周末的逻辑时,您将保存计算以计算每次列出所有请假请求时的日期。

答案 2 :(得分:0)

我不会将'total_days'作为LeaveRequest类中的字段,而是作为属性。

class LeaveRequest(models.Model):
    (other fields)

    @property
    def total_days(self):
        oneday = datetime.timedelta(days=1)
        dt = self.start_date
        total_days = 0
        while(dt <= self.return_date):
            if not dt.isoweekday() in (6, 7):
                total_days += 1
            dt += oneday
        return totaldays

# view function
def leave_screen(request, id):
    # get leave request by id
    leavereq = LeaveRequest.objects.get(id=id)
    return render_to_response("vacation/leave_request.html", {"leavereq": leavereq})

# template code
...
<body>
    {{ leavereq.total_days }}
</body>