I'm fairly new to Django and still trying to figure things out.
I'm trying to create a form to filter courses and populate an HTML dropdown with values from my Postgres database.
My models.py
from django.db import models
# Create your models here.
class AgeGroup(models.Model):
agegroupid = models.AutoField(primary_key=True)
agegrouptitle = models.CharField(max_length=50, blank=True, null=True)
agegroupdescription = models.CharField(max_length=200, blank=True, null=True)
class Meta:
managed = False
db_table = 'age_group'
class Subjects(models.Model):
subjectid = models.AutoField(primary_key=True)
subjecttitle = models.CharField(max_length=50, blank=True, null=True)
subjectdescription = models.CharField(max_length=200, blank=True, null=True)
class Meta:
managed = False
db_table = 'subjects'
class InstructorProfiles(models.Model):
instructorid = models.AutoField(primary_key=True)
instructorname = models.CharField(max_length=50, blank=True, null=True)
instructordegrees = models.CharField(max_length=100, blank=True, null=True)
instructorlongbio = models.CharField(max_length=1000, blank=True, null=True)
instructorshortbio = models.CharField(max_length=500, blank=True, null=True)
instructorphoto = models.CharField(max_length=1000, blank=True, null=True)
class Meta:
managed = False
db_table = 'instructor_profiles'
class Courses(models.Model):
courseid = models.AutoField(primary_key=True)
coursetitle = models.CharField(max_length=100)
# instructorid = models.SmallIntegerField(blank=True, null=True)
coursephoto = models.CharField(max_length=1000, blank=True, null=True)
coursethumbnailphoto = models.CharField(max_length=1000, blank=True, null=True)
credithours = models.DecimalField(max_digits=5, decimal_places=2)
courseoneliner = models.CharField(max_length=200)
coursedescription = models.CharField(max_length=500)
instructor = models.ForeignKey(InstructorProfiles, on_delete=models.CASCADE)
class Meta:
managed = False
db_table = 'courses'
my views.py
from django_mako_plus import view_function, jscontext
from datetime import datetime, timezone
from homepage import models as hmod
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django import forms
#from .forms import FilterForm
@view_function
def process_request(request, cat_id:int = None, pnum:int = 1):
currentDate = datetime.today()
currentYear = currentDate.year
allcourses = hmod.Courses.objects.only("courseid", "coursetitle", "coursephoto", "coursedescription")
form = FilterForm();
context = {
# sent to index.html:
'form': form,
'current_year': currentYear,
'allcourses': allcourses,
}
return request.dmp.render('courselist.html', context)
class FilterForm(forms.Form):
def __init__(self, *args, **kwargs):
super(FilterForm, self).__init__(*args, **kwargs)
ichoices = [(i.instructorid, i.instructorname)
for i in hmod.InstructorProfiles.objects.all()];
print (ichoices)
tchoices = [(t.courseid, t.credithours)
for t in hmod.Courses.objects.all()];
# schoices = [(s.subjectid, s.subjecttitle)
# for s in hmod.Subjects.objects.all()];
#
# achoices = [(a.ageGroupId, a.ageGroupTitle)
# for a in hmod.AgeGroups.objects.all()];
self.fields['Instructor'] = forms.ChoiceField(initial='Select Instructor', choices=ichoices)
# self.fields['Training Hours/Credits'] = forms.ChoiceField(initial='Select Training Hours', choices=tchoices)
# self.fields['Subject'] = forms.ChoiceField(initial='Select Subject', choices=schoices)
# self.fields['Age Group'] = forms.ChoiceField(initial='Select Age Group', choices=achoices)
The error I'm getting is I'm getting the error ProgrammingError at /homepage/courselist/ column courses.instructor does not exist LINE 1: ...."courseoneliner", "courses"."coursedescription", "courses"....
. This only occurs when I try to use the variable tchoices
.
I've double checked that the instructor column does exist, and has data. The instructor column is a foreign key, linking the Courses table to Instructor Profiles. I've run makemigrations
and migrate
, and it tells me there are no changes or unapplied migrations.
I've searched through lots of different questions for similar problems, and I haven't been able to find a solution.
Thanks in advance for your help!