Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

DoesNotExist – matching query does not exist

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()

    # …
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading