使用Django的考勤系统

使用Django的考勤系统,第1张

使用Django的考勤系统

在完成dirkgroten之后,我得以解决问题,回答允许在head_of_department(在hod中登录)下呈现雇员列表并标记相应的出勤率

楷模
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_nameclass 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_nameclass 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)
观看次数
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/')
形式
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'))
模板
{% csrf_token %}{{ formset.management_form }}   {% for form in formset %}      {{ form.employee.initial }} {{ form.employee}}  {{ form.attendance }}<br><br>   {% endfor %}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5096454.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-16
下一篇 2022-11-16

发表评论

登录后才能评论

评论列表(0条)

保存