title: Attendance system using Django
body: <h3>Models</h3>
  
  <pre class="lang-py prettyprint-override"><code>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)
  </code></pre>
  
  <h3>Views</h3>
  
  <pre class="lang-py prettyprint-override"><code>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/')
  </code></pre>
  
  <h3>Forms</h3>
  
  <pre class="lang-py prettyprint-override"><code>class Attendanceform(ModelForm):
      class Meta:
          model = Attendance
          fields = ('employee','attendance','head_of_department')
  
  AttendanceFormset = modelformset_factory(Attendance,fields=('attendance',))
  </code></pre>
  
  <h3>Template</h3>
  
  <pre class="lang-html prettyprint-override"><code>{% csrf_token %}
  {{ formset.management_form }}
      {% for employee in employees %}
          {% for form in formset %}
              {{employee.first_name}} {{ form }}
      {   % endfor %}&lt;br&gt;&lt;br&gt;
      {% endfor %}
  
  </code></pre>
  
  <p>The webapp has a login feature. 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 . </p>
  
  <p>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</p>
  
  <p>Requirement : </p>
  
  <p><a href="https://i.stack.imgur.com/SdlWb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SdlWb.png" alt="enter image description here"></a></p>