【发布时间】:2021-06-10 03:15:20
【问题描述】:
我对 Django 还很陌生,仍在努力解决问题。
我正在尝试创建一个表单来过滤课程并使用我的 Postgres 数据库中的值填充 HTML 下拉列表。
我的模型.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'
我的意见.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)
我得到的错误是我得到了错误ProgrammingError at /homepage/courselist/ column courses.instructor does not exist LINE 1: ...."courseoneliner", "courses"."coursedescription", "courses"....。这只发生在我尝试使用变量tchoices 时。
我已经仔细检查了讲师列确实存在,并且有数据。讲师列是一个外键,将课程表链接到讲师档案。我运行了makemigrations 和migrate,它告诉我没有更改或未应用的迁移。
我已经搜索了很多不同的问题来寻找类似的问题,但我一直无法找到解决方案。
提前感谢您的帮助!
【问题讨论】:
-
> 我运行了
makemigrations和migrate,它告诉我没有更改或未应用的迁移您是否将应用程序添加到 INSTALLED_APPS? -
@Igonato 是的,它已添加到 INSTALLED_APPS
标签: django