如何找到ID日期时间字段与日期字段匹配但其他字段为null的ID?

时间:2018-08-14 22:23:51

标签: django

我有一个模型来跟踪员工的上班/下班应用程序。

class ClockPunch(models.Model):
    employee = models.ForeignKey(settings.AUTH_USER_MODEL)
    date = models.DateField(auto_now_add=True)
    punch_in_time = models.DateTimeField(blank=True, null=True)
    punch_out_time = models.DateTimeField(blank=True, null=True)
    hours_for_the_day = models.DecimalField(max_digits=4, decimal_places=2, default="0.00", blank=True, null=True)

    class Meta:
        ordering = ['date', ]

    def __str__(self):
        return self.pk

和2个视图,其中一个用于时钟输入,一个用于时钟输出...

class ClockPunchInView(LoginRequiredMixin, CreateView):
    context_object_name = "clock_punch"
    fields = ['punch_in_time']
    model = ClockPunch
    template_name = 'pages/layout.html'
    success_url = '/'

    def form_valid(self, form):
        object = form.save(commit=False)
        object.employee = self.request.user
        object.punch_in_time = timezone.now()
        object.save()
        return super(ClockPunchInView, self).form_valid(form)


class ClockPunchOutView(LoginRequiredMixin, UpdateView):
    context_object_name = "clock_punch"
    model = ClockPunch
    template_name = 'pages/layout.html'

    def form_valid(self, form):
        object = form.save(commit=False)
        object.employee = self.request.user
        object.punch_out_time = timezone.now()
        object.save()
        return super(ClockPunchOutView, self).form_valid(form)

这是我的实际表格。我这样做是因为我只希望用户可以在顶部导航栏中按下按钮。我打算稍后再使用此标记...

<form method='POST' action='{% url 'administration:clock_in' %}'>
  {% csrf_token %}
  <input type="submit" style="margin-right:6px;" type="button" class="btn btn-success" value="Clock-in"></button>
</form>
<form method='POST' action='{% url 'administration:clock_out' %}'>
  {% csrf_token %}
  <input type="submit" style="margin-right:6px;" type="button" class="btn btn-danger" value="Clock-out"></button>
</form>

时钟输入可以正常工作,但是我需要以某种方式获取最后一个时钟输入的ID,以便时钟输出视图知道要更新的行。我如何获得上次输入时钟的pk,在输出时钟字段中使用空白或空值?谢谢!

更新: 我会做一些不同的事情,因为我很难更新最后的记录,而没有通过网址传递pk。我只是每次都要创建一个新条目,并使用最后2个条目供用户查找时间增量。 我正在尝试编写一种仅在签入用户时签出用户的方法,但是该方法不起作用。最后一个条目确实具有punched_in = True,但是它的行为好像不是这样,所以问题一定出在我下面的form_valid方法上

class ClockPunchOutView(LoginRequiredMixin, CreateView):
context_object_name = "clock_punch"
model = ClockPunch
fields = ['punch_out_time', 'punched_out',]
#form_class = PunchOutForm
template_name = 'pages/layout.html'
success_url = '/'

def form_valid(self, form):
    object = form.save(commit=False)
    last_entry = ClockPunch.objects.filter(employee=self.request.user).order_by("-punch_in_time").first()

    if last_entry.punched_in:
        object.employee = self.request.user
        object.punch_out_time = timezone.now()
        object.punched_out = True
        object.save()
        return super(ClockPunchOutView, self).form_valid(form)
    else:
        return redirect('/')

1 个答案:

答案 0 :(得分:0)

您可以获得具有最新ClockPunch的该雇员的punch_in_time条目:

object = ClockPunch.objects.filter(employee=self.request.user).order_by("-punch_in_time").first()
object.punch_out_time = timezone.now()
object.save()