smink
smink

Reputation: 37

Django ListView is missing a QuerySet - Error Message - 2 Models In 1 View

I'm trying to view data from 2 models in a single list view. I'm getting the below error message and I can't seem to figure out why. I have included the browser error, views.py, urls.py and html page. Any assistance would be appreciated and as a side note I'm new to programming, Python and Django.

Error: ImproperlyConfigured at /scripts/patientsdetails/ PatientListView is missing a QuerySet. Define PatientListView.model, PatientListView.queryset, or override PatientListView.get_queryset(). Request Method: GET Request URL: http://127.0.0.1:8000/scripts/patientsdetails/ Django Version: 3.2.2 Exception Type: ImproperlyConfigured Exception Value:
PatientListView is missing a QuerySet. Define PatientListView.model, PatientListView.queryset, or override PatientListView.get_queryset().

urls.py

from django.urls import path
from .views import (
    ScriptListView,
    ScriptUpdateView,
    ScriptDetailView,
    ScriptDeleteView,
    ScriptCreateView,
    PatientListView,
    PatientUpdateView,
    PatientDetailView,
    PatientDeleteView,
    PatientCreateView,
)
urlpatterns = [
    path('patientsdetails/', 
        PatientListView.as_view(), name='patient_list'),

]

views.py

from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import ListView, DetailView
from django.views.generic.edit import UpdateView, DeleteView, CreateView
from django.urls import reverse_lazy
from bootstrap_datepicker_plus import DatePickerInput, TimePickerInput
from django.contrib.auth import get_user_model
from django.shortcuts import render
from scripts.models import Patient, Script
from .models import Script, Patient

class PatientListView(LoginRequiredMixin, ListView):
    template_name = 'patient_summary.html'
 
    def get_context_data(self, **kwargs):
        context = super(PatientListView, self).get_context_data(**kwargs).filter(author=self.request.user)
        context['patient'] = Patient.objects.filter(author=self.request.user)
        context['script'] = Script.objects.filter(author=self.request.user)
        return context

html

    {% for patient in object_list %}
        <div class="card">
            <div class="card-header">
                <span class="font-weight-bold">{{ patient.patient_name }}</span> &middot;
                <span class="text-muted"></span>
            </div>
            <div class="card-body">
                Cycle Start Date - {{ patient.cycle_start_day }} <br>
                {% endfor %}

                {% for script in object_list %}
                Drug Name - {{ script.drug_name }} <br>
                {% endfor %}
                </div>
            <div class="card-footer text-center text-muted">
            </div>
        </div>
        <br />

Upvotes: 0

Views: 1417

Answers (1)

Ashish Nautiyal
Ashish Nautiyal

Reputation: 931

I think it's clear from the error that you have to define which model/queryset attribute to work upon by list view Do this in your view:

model = Patient

Or

queryset = Patient.objects.all()

Or override get_queryset method

def get_queryset(self):
        return Patient.objects.all()

Upvotes: 1

Related Questions