0

I'm working on Attendance system in Django. but I'm facing some issue.

model.py

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


class AttendenceTable(models.Model):
    schedule_name = models.ForeignKey(ScheduleTable, on_delete=models.CASCADE, blank=True)
    RollNo = models.CharField(max_length=46, blank=True, null=True)
    student_name = models.TextField(blank=True, null=True)
    status = models.CharField(max_length=8, choices=attendance_choices, blank=True)

    def __str__(self):
        return self.RollNo

** views.py **



def take_attendance(request, id):
    schedule = get_object_or_404(ScheduleTable, pk=id)
    userlist = User.objects.filter(college = schedule.college).filter(is_student=True).filter(section=schedule.section)
    context = {
               "userlist":userlist,
               "schedule_name": schedule,
    }
    return render(request, "take.html", context )


def AttendanceStore(request, id):
    sch = get_object_or_404(ScheduleTable, pk=id)
    userlist = User.objects.all().filter(college=sch.college).filter(section=sch.section)
    attendance = OnlineAttendanceTable()
    insert_list = []
    for user in userlist:
        userstatus = request.POST.get(str(user.username))
        attendance.schedule = sch.name
        attendance.usn = user.username
        attendance.status = userstatus
        insert_list.append(attendance)
        print(insert_list)
        OnlineAttendanceTable.objects.bulk_create()

    return render(request, "submitattendance.html" )

result

enter image description here

As per screenshot, it is replicating data in model but i need one data per user

suggest me any idea to make attendance system in Django

petezurich
  • 9,280
  • 9
  • 43
  • 57
  • What if you change the `userlist` to `userlist = User.objects.filter(college = schedule.college).filter(is_student=True).filter(section=schedule.section).distinct()`? – Willem Van Onsem Jan 13 '20 at 08:09
  • The `OnlineAttendanceTable.objects.bulk_create()` does not make much sense, since here you will bulk create *no* elements at all. – Willem Van Onsem Jan 13 '20 at 08:12

1 Answers1

0

From what I understand, you have a form to take attendance (I would populate a table with the enrolled students' names and a checkbox for each student). I'm assuming the teacher fills the form with the present students and submits it. On submit you should handle the form to update your db.

An example:

In the following pseudo-code I detail the 3 models I would use:

1. ScheduledLecture(models.Model):
    subject -> CharField
    scheduled_date_time -> DateTimeField
   
2. Student(models.Model)
    name -> CharField
    enrolled_lectures -> ManyToManyField(ScheduledLecture)

3. Attendance(models.Model)
    student -> ForeignKey(Student)
    lectures_attended -> ManyToManyField(ScheduledLecture)
    

When you read the Attendance table from the db, you could filter by lecture, and get the students that attended.

Community
  • 1
  • 1
Aerials
  • 4,231
  • 1
  • 16
  • 20
  • You can post your solution as an answer to the question, that way the question is marked as answered. And please try using the editor for the code snippets. Especially for python as indentation is key. Thanks! – Aerials Jan 15 '20 at 10:59