简体   繁体   English

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

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

I have a Django model called Attendance that has the clock in and clock in times of an employee along with the status of that entry, to see whether it's authorized or not. 我有一个名为Attendance的Django模型,该模型具有雇员的进场时间和进场时间以及该条目的状态,以查看其是否被授权。 I then, am making another model called Payroll. 然后,我正在制作另一个称为薪资的模型。 I want this to check inside the Attendance entries to see all the Authorized entries and then do some action on them. 我希望它可以检查“出勤”条目内部以查看所有“授权”条目,然后对它们进行一些操作。 How do I check all the status fields for all the entries in Attendance? 如何检查出勤中所有条目的所有状态字段?

EDIT: Updated to better elaborate my question. 编辑:更新以更好地阐述我的问题。

To better elaborate my question, this is how I've setup my Attendance model: 为了更好地阐述我的问题,这是我如何设置出勤模式:

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,)

If you look closely at the work_entry_status , it's a choice CharField that will contain the status of the entry (UNAUTHORIZED by default). 如果您仔细查看work_entry_status ,则选择CharField它将包含条目的状态(默认情况下为work_entry_status )。

I want to create a Payroll model that will check for all the rows in the CWorkAttendance model and check their work_entry_status fields to see if they are Authorized, which is what I want to learn how to do. 我想创建一个薪资模型,该模型将检查CWorkAttendance模型中的所有行,并检查其work_entry_status字段以查看它们是否已被授权,这就是我想学习的方法。

If those fields are authorized, I want the grab the row's work_employee , work_duration and also some details from the original CEmployees row for the employee. 如果这些字段被授权,我想从雇员的原始CEmployees行中获取行的work_employeework_duration以及一些详细信息。

This is what I want my Payslip/Payroll model to look like: 这是我希望我的薪资单/薪资模型看起来像这样:

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.

Thanks, 谢谢,

Not sure if I understand your requirements well, so let me know if I misunderstood. 不知道我是否很好地理解了您的要求,所以如果我误解了,请告诉我。

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

Update 更新资料

Since you would like to do it manually, I would suggest having a separate view to generate the Payslip. 由于您希望手动进行操作,因此建议您使用一个单独的视图来生成薪资单。 The important thing is to know the date_from and the date_to for this payslip. 重要的是要知道此工资单的date_fromdate_to I imagine that it is the managers who would have access to this view, so you would need the proper access controls set for it. 我想象是经理将有权访问此视图,因此您需要为其设置适当的访问控制。 I also think you need to have a payslip_to_date even if you are going to generate it until the current date, which will be useful for record keeping. 我还认为,即使要在当前日期之前生成它,也需要有一个payslip_to_date ,这对于保存记录很有用。 I assume you have that column in the code below. 我假设您在下面的代码中有该列。

views.py: 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

If you wanted to do it automatically later on, you may want to generate payslips automatically, once a month. 如果您想稍后自动执行此操作,则可能希望每月一次自动生成工资单。 For that you could use something like Celery to have periodic tasks that run in the background, for each employee. 为此,您可以使用Celery之类的方法让每位员工在后台运行定期任务。 If this is the case you could move the above code to a file such as utils.py . 如果是这种情况,您可以将上面的代码移动到utils.py的文件中。 you can create a method which takes employee_id, from_date, to_date, and then generate the payslip object, returning the payslip_id to the calling method 您可以创建一个使用employee_id,from_date,to_date的方法,然后生成payslip对象,将payslip_id返回给调用方法

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

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