gpt4 book ai didi

python - 验证 Django 模型内的 Django 模型字段

转载 作者:太空宇宙 更新时间:2023-11-03 20:30:12 26 4
gpt4 key购买 nike

我有一个名为 attendance 的 Django 模型,其中包含员工的上类时间和上类时间以及该条目的状态,以查看其是否已授权。然后,我正在制作另一个模型,称为工资单。我希望它可以检查出勤条目内部以查看所有授权条目,然后对它们执行一些操作。如何检查考勤中所有条目的所有状态字段?

编辑:更新以更好地阐述我的问题。

为了更好地阐述我的问题,我设置了考勤模型:

class CWorkAttendance(models.Model):
AUTO_ATT = "AU"
MANUAL_ATT = "MA"
WORK_ENTRY_TYPES = (
(AUTO_ATT, "Auto-Attendance"),
(MANUAL_ATT, "Manual-Attendance"),
)
AUTHORIZED = "AU"
UNAUTHORIZED = "UA"
WORK_ENTRY_STATUSES = (
(AUTHORIZED, "Athorized"),
(UNAUTHORIZED, "Un-Authorized"),
)
#Thank you motatoes
def face_locations_in(self, instance):
now = datetime.datetime.now()
return "attendance/{}/{}/in".format(instance.work_employee, now.strftime("%Y/%m/%d"))

def face_locations_out(self, instance):
now = datetime.datetime.now()
return "attendance/{}/{}/out".format(instance.work_employee, now.strftime("%Y/%m/%d"))

work_employee = models.ForeignKey('CEmployees', on_delete=models.CASCADE,)
work_start_time = models.DateTimeField()
work_end_time = models.DateTimeField(null=True)
work_duration = models.IntegerField(null=True)
work_entry_type = models.CharField(max_length=2,choices=WORK_ENTRY_TYPES)
work_entry_status = models.CharField(max_length=2, choices=WORK_ENTRY_STATUSES, default=WORK_ENTRY_STATUSES[1][0])
employee_face_captured_in = models.ImageField(upload_to=face_locations_in,)#////////
employee_face_captured_out = models.ImageField(upload_to=face_locations_out,)

如果您仔细观察 work_entry_status,您会发现它是一个包含条目状态的选择 CharField(默认情况下为 UNAUTHORIZED)。

我想创建一个薪资模型,该模型将检查 CWorkAttendance 模型中的所有行,并检查其 work_entry_status 字段以查看它们是否已授权,这就是授权我想学习怎么做。

如果这些字段已获得授权,我希望获取该行的 work_employeework_duration 以及原始 CEmployees 行中的一些详细信息员工。

这就是我希望我的工资单/工资单模型的样子:

class Payslip(models.Model):
GENERATED = "GEN"
CONFIRMED = "CON"
PAYSLIP_STATUS = (
(GENERATED, "Generated-UNSAVED"),
(CONFIRMED, "Confirmed-SAVED"),
)

payslip_number = models.IntegerField()#MM/YY/AUTO_GENERATED_NUMBER(AUTO_INCREMENT)
payslip_employee = models.ForeignKey('CEmployees', on_delete=models.CASCADE,)#Choose the employee from the master table CEmployees
payslip_generation_date = models.DateTimeField(default=datetime.datetime.now())#Date of the payroll generation
payslip_total_hours = models.IntegerField()#Total hours that the employee worked
payslip_from_date = models.DateField()"""The date from when the payslip will be made. The payslip will be manual for now, so generate it after choosing a a date to generate from."""
payslip_total_basic_seconds = models.IntegerField()#Total seconds the employee worked
payslip_total_ot_seconds = models.IntegerField()#Total overtime seconds the employee worked
payslip_basic_hourly_rate = models.IntegerField()#The basic hourly rate of the employee mentioned here. Take from the master employees table.
payslip_basic_ot_rate = models.IntegerField()#Taking the basic overtime rate from the master table
payslip_total_amount = models.FloatField()#The total amount of the payslip
payslip_entry_status = models.CharField(max_length=3, default=GENERATED)#The status of the pay slip.

谢谢

最佳答案

不确定我是否很好地理解了您的要求,所以如果我误解了,请告诉我。

# `employee` is the work_employee in question

# if you don't want to filter by employee, remove `work_employee=employee`
attendances = CWorkAttendance.objects.filter(work_entry_status=CWorkAttendance.AUTHORIZED, work_employee=employee)

for attendances in attendances:
# do things with this attendance record
attendance.work_duration
attendance.employee
# ....

更新

由于您想手动执行此操作,我建议使用单独的 View 来生成工资单。重要的是要知道此工资单的 date_fromdate_to。我想管理者有权访问此 View ,因此您需要为其设置适当的访问控制。我还认为您需要有一个 payslip_to_date 即使您要在当前日期之前生成它,这对于保存记录很有用。我假设您在下面的代码中有该列。

views.py:

from django.views import View


class GeneratePayslip(View):
"""
make sure you have the right access controls set to this view
"""
def post(self, request, **kwargs):
employee_id = kwags.POST.get("employee_id")
date_from = kwargs.POST.get("from_date")
date_to = kwargs.POST.get("to_date")

# we fetch all the objects within range
attendances = CWorkAttendance.objects.filter( \
work_entry_status=CWorkAttendance.AUTHORIZED, \
work_employee_id=employee_id, \
work_start_time__gte=date_from, \
work_end_time__lte=date_to \
)

hours = 0
for attendance in attendances:
# perform calculations to compute total sum and rate
pass

# create the payslip object here ..

# redirect to a success page and return

如果您想稍后自动执行此操作,您可能需要每月自动生成一次工资单。为此,您可以使用 Celery 之类的工具为每个员工在后台运行定期任务。如果是这种情况,您可以将上述代码移动到诸如 utils.py 之类的文件中。您可以创建一个接受employee_id、from_date、to_date的方法,然后生成工资单对象,将工资单_id返回给调用方法

关于python - 验证 Django 模型内的 Django 模型字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57558169/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com