我有一个可以以签入/签出方式进行工作的应用程序。人们只需输入其ID,区域,然后单击签入,它会将时间戳及其信息发送给数据库。还有一个“签出”按钮当前不执行任何操作。应该可以这样工作,一旦该人填写了表单并提交了,它将执行以下任一操作:
我认为尝试通过同时匹配ID和区域来查找用户会更好,因为同一个人一天可以多次签入/签出不同区域,因此仅按ID进行匹配可能会导致程序更新在某一点上出现了错误的场。
models.py
class WorkArea(models.Model):
name = models.CharField(max_length=50)
def __str__(self):
return self.name
class StationNumber(models.Model):
work_area = models.ForeignKey(WorkArea, on_delete=models.CASCADE)
name = models.CharField(max_length=50)
def __str__(self):
return self.name
class EmployeeWorkAreaLog(TimeStampedModel, SoftDeleteModel, models.Model):
employee_number = models.IntegerField(max_length=50, help_text="Employee #", blank=False)
work_area = models.ForeignKey(WorkArea, on_delete=models.SET_NULL, null=True, blank=False, help_text="Work Area", related_name="work_area")
station_number = models.ForeignKey(StationNumber, on_delete=models.SET_NULL, null=True, help_text="Station", related_name="stations", blank=True)
time_in = models.DateTimeField(auto_now_add=True, help_text="Time in", null=True, blank=True)
time_out = models.DateTimeField(blank=True, help_text="Time out", null=True)
def __str__(self):
return self.employee_number
forms.py
class EnterExitArea(CreateView):
model = EmployeeWorkAreaLog
template_name = "operations/enter_exit_area.html"
form_class = WarehouseForm
def form_valid(self, form):
form.save()
return HttpResponseRedirect(self.request.path_info)
def load_stations(request):
work_area_id = request.GET.get('work_area')
stations = StationNumber.objects.filter(work_area_id=work_area_id).order_by('name')
return render(request, 'operations/station_number_dropdown_options.html', {'stations': stations})
我想我的问题更多地是关于如何实现这样的事情?
而且,当前签入会在提交时自动保存时间戳,但是在按下签出区域按钮时不应更改它,因为这样做会破坏其目的。
答案 0 :(得分:0)
l_userID = int(<feed the user id>)
l_areaID = <feed the area id>
l_stationId =<feed the station id>
l_objLogEntry = EmployeeWorkAreaLog.objects.filter(employee_number=l_userID,\
work_area__id = l_areaID,\
station_number__id = l_stationId)\
.exclude(time_out__isnull=True,\
time_out__exact='')
if len(l_objLogEntry) == 0:
#no login entry
elif len(l_objLogEntry) == 1:
#update the timestamp
elif len(l_objLogEntry) > 1:
#multiple open entries, do whatever