在 urls.py 中先导入getgrades
from django.conf.urls import url
from app5 import views
urlpatterns = [
url(r'getuser/',views.get_user),
url(r'getusers/',views.get_users),
url(r'getorder',views.getorders),
url(r'getgrades',views.getgrades),
]
在 views.py 中编写 getgrades 函数
def getgrades(request):
grades = Grade.objects.filter(student__s_name='qq')
# 班级里包含名字为 qq 的
for grade in grades:
print(grade.g_name)
return HttpResponse("获取成功")
此前的准备工作:创建数据库表
class Grade(models.Model):
g_name = models.CharField(max_length=16)
class Student(models.Model):
s_name = models.CharField(max_length= 16)
s_grade = models.ForeignKey(Grade,on_delete=True)
# 设置班级外键
2020-05-11