Home > OS >  DoesNotExist - matching query does not exist
DoesNotExist - matching query does not exist

Time:12-04

I have this django class-based view where i am trying to overwrite the get_queryset function because i want get to the inserted values from the frontend to search in my database after the subject with that name and then get the id. but when i call the view it gives me a "Subject matching query does not exist." because the subject_val is None. That makes sense because the user has not submitted the values jet.. so how do i get it to wait until a user have choosen "submit

class AttendanceList(LoginRequiredMixin, ListView):
    model = AttendanceLog
    template_name = "./attendancecode/showattendance.html"


    def get_queryset(self):
       class_val = self.request.GET.get('class')
       subject_val = self.request.GET.get('subject')
       sub = Subject.objects.get(name=subject_val).id
       new_context = get_statstic(class_val, sub)
       return new_context

    def get_context_data(self, **kwargs):
        context = super(AttendanceList, self).get_context_data(**kwargs)
        context['class'] = self.request.GET.get('class')
        context['subject'] = self.request.GET.get('subject')
        return context
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You can check if the values are not None, in case they are, you need to return another queryset of AttendanceLogs (for example AttendanceLog.objects.all() or AttendanceLog.objects.none()):

class AttendanceList(LoginRequiredMixin, ListView):
    model = AttendanceLog
    template_name = "./attendancecode/showattendance.html"

    def get_queryset(self):
       class_val = self.request.GET.get('class')
       subject_val = self.request.GET.get('subject')
       if class_val is not None and subject_val is not None:
           sub = Subject.objects.get(name=subject_val).id
           return get_statstic(class_val, sub)
       # return another queryset:
       return AttendanceLog.objects.none()

    # …
  • Related