0

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

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

有什么建议么?

4

3 回答 3

1

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

如果您只想查看一条休假记录,则需要传递LeaveRequest

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 })
于 2013-01-11T21:05:10.097 回答
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']

这样,当您输入排除周末的逻辑时,您可以节省计算以计算每次列出所有休假请求时的天数。

于 2013-01-11T21:31:31.593 回答
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>
于 2013-01-11T21:17:59.157 回答