Home > Mobile >  any I'm trying to update a booleanfield from another model. i have 2 model studentcourse and Ex
any I'm trying to update a booleanfield from another model. i have 2 model studentcourse and Ex

Time:02-02

I have 2 models which are StudentCourses and ExamGrade, below are the models and attribute important which i would like to update on StudentCourses when ExamGrade is created

models.py

class StudentCourses(models.Model):
   id = models.AutoField(primary_key=True)
   student = models.ForeignKey(Students, related_name='studentcourses')
   studentcourse = models.ForeignKey(Courses, related_name='studentcourses', null=True)
   show = models.BooleanField(default=False)

class ExamGrade(models.Model):
    id = models.AutoField(primary_key=True)
    lecturer = models.ForeignKey(Lecturers, null=True, related_name='examgrades')
    student = models.ForeignKey(StudentCourses, related_name='examgrades')

class Students(models.Model):
   id = models.AutoField(primary_key=True)
   admin = models.OneToOneField(CustomUser, on_delete=models.CASCADE, related_name="students")
   profile_pic = models.ImageField(default='default.jpg', upload_to='uploads/student')
   middle_name = models.CharField(max_length=255)
   gender = models.CharField(max_length=255)
   status = models.CharField(max_length=255, null=True)
country = models.CharField(max_length=255, default=None, null=True, blank=True)
   state = models.CharField(max_length=255, default=None, null=True, blank=True)
   local = models.CharField(max_length=255, default=None, null=True, blank=True)
   school = models.ForeignKey(School, null=True, on_delete=models.CASCADE)
   dob = models.CharField(max_length=100, default=None, blank=True, null=True)
   guardian_name = models.CharField(max_length=11, null=True)
   mobile = models.CharField(max_length=12, null=True, default=None)
   faculty = models.ForeignKey(Faculty, null=True, blank=True, on_delete=models.CASCADE)
   department = models.ForeignKey(Department, null=True, blank=True, on_delete=models.CASCADE)
   level = models.ForeignKey(Level, null=True, blank=True, on_delete=models.CASCADE)
   semester = models.ForeignKey('Semester', null=True, blank=True, on_delete=models.CASCADE)
   session = models.ForeignKey(SessionYearModel, null=True, blank=True, on_delete=models.CASCADE)
   created_at = models.DateTimeField(auto_now_add=True)
   updated_at = models.DateTimeField(auto_now_add=True)

   @property
   def examgrades(self):
      return self.examgrades_set.all().order_by('student')

   @property
   def studentcourses(self):
      return self.studentcourses_set.all().order_by('studentcourse')

from the model above the student are related to both StudentCourse and ExamGrade i would like to update the show attribute to True on StudentCourses and in my views i have this code below

Views.py

class CreateGradeView(View):
  def get(self, request, *args, **kwargs):
    lecturer= Lecturers.objects.get(admin=request.user)
    course = LecturerCourse.objects.filter(lecturer=lecturer)
    context = {
        'course':course,
        'room_name': "broadcast"
        }
    return render(request, 'superadmin/lecturer/add-grade.html',context)

  def post(self, request, *args, **kwargs):
    lecturer = request.POST.get('lecturer')
    lecturer = Lecturers.objects.get(admin=lecturer)
    student = request.POST.get('student')
    student = StudentCourses.objects.get(id=student)
    course = request.POST.get('lecturercourse')
    course = LecturerCourse.objects.get(id=course)
    ca_1 = request.POST.get('ca_1')
    ca_2 = request.POST.get('ca_2')
    ca_3 = request.POST.get('ca_3')
    exam = request.POST.get('exam')

    if ExamGrade.objects.filter(lecturer=lecturer, student=student, lecturercourse=course, status=True).exists():
        messages.warning(request, "User Exam Score already marked")
        return redirect('emisapp:add-grade')
    
    try:
        examgrades = ExamGrade(lecturer=lecturer, student=student, lecturercourse=course, ca_1=ca_1,ca_2=ca_2,ca_3=ca_3,exam=exam, status=True)
        print(examgrades.student.show) 
        examgrades.student.show = True
        print(examgrades.student.show) 
        examgrades.save()
        messages.success(request, 'Weldone you just saved Exam Score for the day')
        return redirect('emisapp:add-grade')
    except:
        messages.error(request, 'There is an saving to database')
        return redirect('emisapp:add-grade')

ajax.py

def load_student_for_attendance_by_courses(request):
  studentcourse = request.GET.get('studentcourse')
  student = StudentCourses.objects.filter(studentcourse=studentcourse, 
  show=False).order_by('student')
  return render(request, 'superadmin/settings/load-student- 
  dropdown.html', {'student':student})

load-student-dropdown.html

<option value="">--Select a Student--</option>
{% for s in student %}
<option value="{{s.id}}">{{ s.student.admin.username }} - {{ s.student.admin.last_name }} {{ s.student.middle_name }} {{ s.student.admin.first_name }}</option>

{% endfor %}

and this was my error 'RelatedManager' object has no attribute 'show'.

CodePudding user response:

Now you can simply do this:

from django.shortcuts import get_object_or_404

try:
    examgrades = ExamGrade(lecturer=lecturer, student=student, lecturercourse=course,
                        ca_1=ca_1, ca_2=ca_2, ca_3=ca_3, exam=exam, status=True)
    print(examgrades.student.show)
    stu_courses_instance = get_object_or_404(
        StudentCourses, id=examgrades.student.id)
    stu_courses_instance.show = True
    stu_courses_instance.save()
    print(examgrades.student.show)
    examgrades.save()
    messages.success(request, 'Weldone you just saved Exam Score for the day')
    return redirect('emisapp:add-grade')

Note: Generally, Django models are written in PascalCase and don't require s to be the suffix as it is added by default by Django, so it is better to name the models as StudentCourse, 'Student and 'Lecturer not StudentCourses,Students and Lecturers respectively.

  • Related