繁体   English   中英

验证Django模型中的Django模型字段

[英]Verify a Django model field inside a Django model

我有一个名为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它将包含条目的状态(默认情况下为work_entry_status )。

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

如果这些字段被授权,我想从雇员的原始CEmployees行中获取行的work_employeework_duration以及一些详细信息。

这是我希望我的薪资单/薪资模型看起来像这样:

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
    # ....

更新资料

由于您希望手动进行操作,因此建议您使用一个单独的视图来生成薪资单。 重要的是要知道此工资单的date_fromdate_to 我想象是经理将有权访问此视图,因此您需要为其设置适当的访问控制。 我还认为,即使要在当前日期之前生成它,也需要有一个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的方法,然后生成payslip对象,将payslip_id返回给调用方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM