2

I am a beginner in django. Following is my Attendance Management App. This is my models.py.

from django.db import models

class Subject(models.Model):
    subject_name = models.CharField(max_length=20)
    #attendance = models.ForeignKey(Attendance, on_delete = 
    models.DO_NOTHING)
    attendance = models.IntegerField(default=0)
    def __str__(self):
        return self.subject_name


class Section(models.Model):
    section_name = models.CharField(max_length=20)
    subject = models.ManyToManyField(Subject)
    def __str__(self):
        return self.section_name


class Student(models.Model):
    rollno = models.IntegerField()
    name = models.CharField(max_length=20)
    section = models.ForeignKey(Section, on_delete = models.DO_NOTHING, 
    default=0)
    def __str__(self):
        return str(self.rollno) + self.name 


class Teacher(models.Model):
    #teacher_name = models.CharField(max_length=20)
    section = models.ForeignKey(Section, on_delete=models.CASCADE)
    subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
    #section_name = models.CharField(max_length=10)
    #subject_name = models.CharField(max_length=30)
    def __str__(self):
        return self.section.section_name+' '+self.subject.subject_name


class TeacherList(models.Model):
    teacher_name = models.CharField(max_length=20)
    teacher = models.ManyToManyField(Teacher)
    def __str__(self):
        return self.teacher_name

The line below is not working in views.py

student = models.Student.objects.get(rollno = sroll)
student.section.subject.get(subject_name = 'java').attendance += 1
student.save()

This view is called from a template when a button is clicked to add attendance of a student. The attendance in above code is not modified. Please help me out.

2
  • Is it because in your class Subject you have a random line of code "models.DO_NOTHING)" that should be commented out? When you say it doesn't work does it have an error or does it just not save the student instance? Commented Jul 7, 2018 at 18:25
  • There is no error. But the attendance is not incremented. Commented Jul 7, 2018 at 19:31

1 Answer 1

2

You're saving the student, instead of the subject.

student = models.Student.objects.get(rollno = sroll)
subject = student.section.subject.get(subject_name='java')
subject.attendance += 1
subject.save()

Or, better, do the update directly in one go:

student.section.subject.filter(subject_name='java').update(attendance=F('attendance')+1)

and there's no need to save at all.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Daniel Roseman. But this code incremented attendance of whole class corresponding to subject 'java'. Maybe my model is wrongly designed. Please suggest me something that should work here.
Well yes, you would need some kind of Attendance model as the through table in a relationship between Student and Subject. But I can't really give a proper answer as I don't understand what your Section model is for.
Students belong to a Section which may have 5-6 subjects. And each subject is taught by a teacher. Please suggest me relationships between these models so that attendance of each student could be marked for every subject individually.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.