How do i retrieve Course objects using semId as the search key…
Also using filter gives empty queryset…TIA
models.py
class Semester(models.Model):
semId=models.IntegerField(unique=True)
session=models.DecimalField(max_digits=4,decimal_places=0,null=True)
def __str__(self):
return str(self.semId)
class Course(models.Model):
course=models.Manager()
semester=models.ForeignKey(Semester,on_delete=models.CASCADE,null=True)
courseName=models.CharField(max_length=100)
courseId=models.IntegerField(unique=True)
student=models.ManyToManyField(Student)
def __str__(self):
return str(self.courseName)
views.py
def sem(request,id):
ob=Course.course.get(semester=id)
for o in ob:
print(o.semester)
return render(request,'sem.html')
Error using get
DoesNotExist at /sem/8/
Course matching query does not exist.
Request Method: GET
Request URL: http://127.0.0.1:8000/sem/8/
Django Version: 4.1.1
Exception Type: DoesNotExist
Exception Value:
Course matching query does not exist.
Exception Location: C:\Users\ASUS\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py, line 650, in get
Raised during: emnei.views.sem
Python Executable: C:\Users\ASUS\AppData\Local\Programs\Python\Python310\python.exe
Python Version: 3.10.7
Python Path:
['C:\\Users\\ASUS\\Dropbox\\PC\\Downloads\\dd\\project_2\\project\\uosmoy',
'C:\\Users\\ASUS\\AppData\\Local\\Programs\\Python\\Python310\\python310.zip',
'C:\\Users\\ASUS\\AppData\\Local\\Programs\\Python\\Python310\\DLLs',
'C:\\Users\\ASUS\\AppData\\Local\\Programs\\Python\\Python310\\lib',
'C:\\Users\\ASUS\\AppData\\Local\\Programs\\Python\\Python310',
'C:\\Users\\ASUS\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages']
Server time: Sat, 17 Sep 2022 12:24:47 +0000