0

"allotted_pto" (有給休暇) が UserProfile モデルの整数フィールド (日数を表す) の場合:

class UserProfile(models.Model):  
    user = models.ForeignKey(User, unique=True)
    fullname = models.CharField(max_length=64, unique=False)
    company = models.CharField(max_length=50, choices=CLIENT_CHOICES)
    ...
    allotted_pto = models.IntegerField(max_length=2, blank=True, null=True)
    ...

    User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])

「total_days」は、休暇申請モデルから整数を返します。

class LeaveRequest(models.Model):
    employee = models.ForeignKey(UserProfile)
    supervisor = models.ForeignKey(UserProfile, related_name='+', blank=False, null=False)
    ...
    total_days = models.IntegerField(max_length=2, blank=True, null=True)

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

    def save(self, *args, **kwargs):
            fromdate = self.start_date
            todate = self.return_date
            daygenerator = (fromdate + timedelta(x + 1) for x in xrange((todate - fromdate).days))
            self.total_days = sum(1 for day in daygenerator if day.weekday() < 5)
            super(LeaveRequest, self).save(*args, **kwargs)
            ...

レコードのフィルター セットから "total_days" の合計を取得し、その合計をユーザー プロファイルの "allotted_pto" から差し引くビューを作成するにはどうすればよいですか? 私が書いた単純なビュー (以下を参照) は、実際の日数をカウントするのではなく、「total_days」オブジェクトの数を (辞書形式で) 生成します。また、「allotted_pto」のリクエストは、何も返さないため、明らかに正しく構築されていません...

#views.py 

def leave_screen(request, id):
    profile = UserProfile.objects.get(user=id)
    records = LeaveRequest.objects.filter(employee=id)
    agg_pto = LeaveRequest.objects.aggregate(Count('total_days'))
    if profile.allotted_pto: #if the allotted_pto field in UserProfile is not empty
            allotted_pto = profile.allotted_pto
            remaining_pto = allotted_pto - agg_pto
    else:
            remaining_pto = "na"
    return render_to_response("vacation/leave_request.html", {'records': records, 'agg_pto': agg_pto, 'remaining_pto': remaining_pto})

わかりました、計算を考え出しました:

def leave_screen(request, id):
    ...
    agg_pto = LeaveRequest.objects.filter(employee=id).aggregate(Sum('total_days'))
    agg_pto = agg_pto['total_days__sum']

ユーザー プロファイル モデルから allotted_pto 整数を取得する方法を理解する必要があります。

4

1 に答える 1

1

わかりました、これは思ったほど難しくありませんでした。最初の課題は、オブジェクトの総和を取得することでした。私の最初の試みは近いものでしたが、「カウント」ではなく「合計」を使用する必要がありました。

agg_pto = LeaveRequest.objects.filter(employee=id).aggregate(Sum('total_days'))

次に、辞書から値を抽出するために python メソッドを使用しました。

agg_pto = agg_pto['total_days__sum']

最後に:

def leave_screen(request, id):
    user = request.user.id
    profile = request.user.get_profile()
    records = LeaveRequest.objects.filter(employee=id).order_by('-submit_date')
    agg_pto = LeaveRequest.objects.filter(employee=id).aggregate(Sum('total_days'))
    agg_pto = agg_pto['total_days__sum']
    allotted_pto = profile.allotted_pto
    if allotted_pto: #if the allotted_pto field in UserProfile is not empty
            remaining_pto = allotted_pto - agg_pto
    else:
            remaining_pto = "na"
    supervised_records = LeaveRequest.objects.filter(supervisor=id).order_by('-submit_date')
    return render_to_response("vacation/leave_request.html", {'records': records, 'supervised_records': supervised_records, 'agg_pto': agg_pto, 'allotted_pto': allotted_pto, 'remaining_pto': remaining_pto, 'profile': profile })

UserProfile からオブジェクトを取得するための構文を理解するのがなぜそれほど困難だったのか、私にはわかりません。しかし、django-debug-toolbar が非常に役立つことは知っています。

于 2013-01-28T22:13:55.220 に答える