Django 数据模型
一对一关系
学生和学生证关系
class Student(models.Model):
sno = models.AutoField(primary_key=True)
sname = models.CharField(max_length=30)
def __unicode__(self):
return u'Student:%s'%self.sname
class Scard(models.Model):
stu = models.OneToOneField(Student,primary_key=True,on_delete=models.CASCADE)
major = models.CharField(max_length=30,unique=True)
def __unicode__(self):
return u'Scard:%s'%self.major
#插入数据
s1 = Student.objects.create(sname='zhangsan')
s2 = Student.objects.create(sname='lisi')
Scard.objects.create(stu=s1,major='数学')
<Scard: [Bad Unicode data]>
Scard.objects.create(stu=s2,major='语文')
<Scard: [Bad Unicode data]>
#查询数据 正向查询 scard 为 models 里面的类,查询时候用类小写
Student.objects.first().scard
<Scard: Scard:数学>
#查新数据 逆向查询 stu 为 Scard类中的属性,也就是字段
Scard.objects.first().stu
<Student: Student:zhangsan>
一对多关系
班级和学生
#班级主表 一
class Clazz(models.Model):
cno = models.AutoField(primary_key=True)
cname = models.CharField(max_length=30)
def __unicode__(self):
return u'Clazz:%s'%self.cname
#学生表 从表 多
class Student(models.Model):
sno = models.AutoField(primary_key=True)
sname = models.CharField(max_length=30)
cno = models.ForeignKey(Clazz,on_delete=models.CASCADE)
def __unicode__(self):
return u'Student:%s'%self.sname
from stu.models import *
# 插入数据
cls1 = Clazz.objects.create(cname='python')
cls2 = Clazz.objects.create(cname='html')
Student.objects.create(sname='zhangsan',cno=cls1)
<Student: Student:zhangsan>
Student.objects.create(sname='lisi',cno=cls1)
<Student: Student:lisi>
Student.objects.create(sname='wangwu',cno=cls2)
<Student: Student:wangwu>
#查询数据 正向查询 一个班级有多个学生
Clazz.objects.first().student_set.all()
<QuerySet [<Student: Student:zhangsan>, <Student: Student:lisi>]>
# 查询数据 逆向查询 一个学生是属于一个班级
Student.objects.first().cno
<Clazz: Clazz:python>
写一个方法可以批量插入数据
def insertData1(clsname,*snames):
try:
cls = Clazz.objects.get(cname=clsname)
except Clazz.DoesNotExist:
cls = Clazz.objects.create(cname=clsname)
for i in snames:
try:
stu = Student.objects.get(sname=i)
except Student.DoesNotExist:
Student.objects.create(sname=i,cno=cls)
#调用函数
insertData1('k8s','zhangjie','xiena')
多对多关系
课程和教师
#课程表
class Course(models.Model):
course_no = models.AutoField(primary_key=True)
course_name = models.CharField(max_length=30)
def __unicode__(self):
return u'Course:%s'%self.course_name
#教师表
class Teacher(models.Model):
tno = models.AutoField(primary_key=True)
tname = models.CharField(max_length=30)
cour = models.ManyToManyField(Course)
def __unicode__(self):
return u'Teacher:%s'%self.tname
# 课程表插入数据
course1 = Course.objects.create(course_name='Python')
course2 = Course.objects.create(course_name='Java')
course3 = Course.objects.create(course_name='Html5')
#教师表插入数据
t1 = Teacher.objects.create(tname='zhangsan')
t2 = Teacher.objects.create(tname='lisi')
#教师表插入课程 与一对多是有区别的
t1.cour.add(course1,course2,course3)
#正向查询 是集合列表
Course.objects.first().teacher_set.all()
<QuerySet [<Teacher: Teacher:zhangsan>]>
#逆向查询 也是集合列表
Teacher.objects.first().cour.all()
<QuerySet [<Course: Course:Python>, <Course: Course:Java>, <Course: Course:Html5>]>
#写一个方法可以批量插入数据:
def inserDataN(teachername,*coursename):
try:
t = Teacher.objects.get(tname=teachername)
except Teacher.DoesNotExist:
t = Teacher.objects.create(tname=teachername)
for i in coursename:
try:
course = Course.objects.get(course_name=i)
except:
course = Course.objects.create(course_name=i)
t.cour.add(course)
# 方法调用
inserDataN('dong','css','js')