简体   繁体   English

使用Django的考勤系统

[英]Attendance system using Django

Models 楷模

attendance_choices = (
    ('absent', 'Absent'),
    ('present', 'Present')
)

class Head_of_department(models.Model):
    first_name = models.CharField(max_length=200)
    last_name = models.CharField(max_length=200)
    email = models.CharField(max_length=30)

    def __str__(self):
        return self.first_name 

class Employee(models.Model):
    first_name = models.CharField(max_length=200, unique=True)
    last_name = models.CharField(max_length=200, unique=True)
    head_of_department = models.ForeignKey('Head_of_department', on_delete=models.SET_NULL, blank=True, null=True)
    email = models.EmailField(max_length=100)

     def __str__(self):
        return self.first_name + ' ' + self.last_name

class Attendance(models.Model):
    head_of_department = models.ForeignKey('Head_of_department', on_delete=models.SET_NULL, blank=True, null=True)
    employee = models.ForeignKey('Employee', on_delete=models.CASCADE, )
    attendance = models.CharField(max_length=8, choices=attendance_choices, blank=True)

Views 查看

class Attendancecreate(CreateView):
    model = Attendance
    fields = ['employee']
    success_url = '/dashboard/'

    def get_context_data(self,** kwargs):
        context = super(Attendancecreate, self).get_context_data(**kwargs)
        context['formset'] = AttendanceFormset(queryset=Attendance.objects.none())
        context['attendance_form'] = Attendanceform()
        email = self.request.user.email
        hod = Head_of_department.objects.get(email=email)
        context["employees"] = Employee.objects.filter(head_of_department =hod)
        return context

    def get_initial(self):
        email = self.request.user.email
        hod = Head_of_department.objects.get(email=email)
        initial = super(Attendancecreate , self).get_initial()
        initial['employee'] = Employee.objects.filter(head_of_department=hod)
        return initial

    def post(self, request, *args, **kwargs):
        formset = AttendanceFormset(request.POST)
        if formset.is_valid():
            return self.form_valid(formset)

     def form_valid(self, formset):
        instances = formset.save(commit=False)
        for instance in instances:
            instance.head_of_department = get_object_or_404(Head_of_department, email=self.request.user.email)
            instance.save()
        return HttpResponseRedirect('/dashboard/')

Forms 形式

class Attendanceform(ModelForm):
    class Meta:
        model = Attendance
        fields = ('employee','attendance','head_of_department')

AttendanceFormset = modelformset_factory(Attendance,fields=('attendance',))

Template 模板

{% csrf_token %}
{{ formset.management_form }}
    {% for employee in employees %}
        {% for form in formset %}
            {{employee.first_name}} {{ form }}
    {   % endfor %}<br><br>
    {% endfor %}

The webapp has a login feature. webapp具有登录功能。 The headofdepartment can mark the attendance . 总部可以标记出勤率。 List of employees are rendered in the template without any issues , I want to mark attendance to the respective employees sorted in ascending order of their first_name . 员工列表在模板中呈现没有任何问题,我想标记出现在各个员工的出勤率,按照他们的first_name的升序排序。

That is when marking attendance employees will be listed in template, and to the right attendance form will be displayed for all the employees . 那时标记出勤员工将被列入模板,并且将为所有员工显示正确的出勤表格。 It is saving only one object and not assigning the initial value for employee 它只保存一个对象而不为员工分配初始值

Requirement : 要求:

在此输入图像描述

Following dirkgroten I was able to solve the issue, answer allow to render a list employees under the head_of_department(logged in hod) and mark respective attendance . dirkgroten之后,我能够解决问题,回答允许在head_of_department(登录hod)下呈现员工列表并标记相应的出勤率。

Models 楷模

attendance_choices = (
    ('absent', 'Absent'),
    ('present', 'Present')
)

class Head_of_department(models.Model):
    first_name = models.CharField(max_length=200)
    last_name = models.CharField(max_length=200)
    email = models.CharField(max_length=30)

    def __str__(self):
        return self.first_name 

class Employee(models.Model):
    first_name = models.CharField(max_length=200, unique=True)
    last_name = models.CharField(max_length=200, unique=True)
    head_of_department = models.ForeignKey('Head_of_department', on_delete=models.SET_NULL, blank=True, null=True)
    email = models.EmailField(max_length=100)

     def __str__(self):
        return self.first_name + ' ' + self.last_name

class Attendance(models.Model):
    head_of_department = models.ForeignKey('Head_of_department', on_delete=models.SET_NULL, blank=True, null=True)
    employee = models.ForeignKey('Employee', on_delete=models.CASCADE, )
    attendance = models.CharField(max_length=8, choices=attendance_choices, blank=True)

Views 查看

class Attendancecreate(CreateView):
    model = Attendance
    form_class = Attendanceform
    success_url = '/dashboard/'

    def get_context_data(self,** kwargs):
        context = super(Attendancecreate, self).get_context_data(**kwargs)
        context['formset'] = AttendanceFormset(queryset=Attendance.objects.none(), instance=Head_of_department.objects.get(email=self.request.user.email), initial=[{'employee': employee} for employee in self.get_initial()['employee']])
        return context

    def get_initial(self):
        email = self.request.user.email
        head_of_department = Head_of_department.objects.get(email=email)
        initial = super(Attendancecreate , self).get_initial()
        initial['employee'] = Employee.objects.filter(head_of_department=head_of_department)
        return initial

    def post(self, request, *args, **kwargs,):
        formset = AttendanceFormset(request.POST,queryset=Attendance.objects.none(), instance=Head_of_department.objects.get(email=self.request.user.email), initial=[{'employee': employee} for employee in self.get_initial()['employee']])
        if formset.is_valid():
            return self.form_valid(formset)

    def form_valid(self,formset):
        instances = formset.save(commit=False)
        for instance in instances:
            instance.head_of_department = get_object_or_404(Head_of_department, email=self.request.user.email)
            instance.save()
        return HttpResponseRedirect('/dashboard/')

Forms 形式

class Attendanceform(ModelForm):
    class Meta:
        model = Attendance
        widgets = {'employee' : HiddenInput}
        fields = ('employee','attendance','hod')
AttendanceFormset = inlineformset_factory(Head_of_department,Attendance,form=Attendanceform,fields=('attendance','employee'))

Template 模板

{% csrf_token %}
{{ formset.management_form }}
   {% for form in formset %}
      {{ form.employee.initial }} {{ form.employee}}  {{ form.attendance }}
<br><br>
   {% endfor %}

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

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