----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
步骤:
http://127.0.0.1:8000/student/
http://127.0.0.1:8000/student/showall/
http://127.0.0.1:8000/student/getstu/?cno=1
----------------------------------------------------------------------------------
models.py
from django.db import models
# Create your models here.
class Clazz(models.Model):
cno = models.AutoField(primary_key=True)
cname = models.CharField(max_length=30)
class Course(models.Model):
course_no = models.AutoField(primary_key=True)
course_name = models.CharField(max_length=30)
class Student(models.Model):
sno = models.AutoField(primary_key=True)
sname = models.CharField(max_length=30)
cls = models.ForeignKey(Clazz, on_delete=models.CASCADE)
cour = models.ManyToManyField(Course)
# 根据班级名称获取班级对象
def getCls(cname):
try:
cls = Clazz.objects.get(cname=cname)
except Clazz.DoesNotExist:
cls = Clazz.objects.create(cname=cname)
return cls
# 获取课程对象列表
def getCourseList(*coursenames):
courseList = []
for cn in coursenames:
try:
c = Course.objects.get(course_name=cn)
except Course.DoesNotExist:
c = Course.objects.create(course_name=cn)
courseList.append(c)
return courseList
def registerStu(sname, cname, *coursenames):
# 1.获取班级对象
cls = getCls(cname)
# 2.获取课程对象列表
courseList = getCourseList(*coursenames)
# 3.插入学生表数据
try:
stu = Student.objects.get(sname=sname)
except Student