使用OneToOneField()
讲的应该是主键关联的东西,主从关系
学生证学号关联学生学号,学号修改随之修改。
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
# Create your models here.
# 学生 和 学生证的关系 1:1
class Student(models.Model):
#学号
sno = models.AutoField(primary_key=True)
#学生姓名
sname =models.CharField(max_length=30,unique=True)
#给表去个名字吧
class Meta:
db_table='t_student'
打印输出的东西编码打印
def __unicode__(self):
return u'%s'%self.sname
class Scard(models.Model):
#学号
sno = models.OneToOneField(Student,primary_key=True,on_delete=models.CASCADE)
department= models.CharField(max_length=50)
major = models.CharField(max_length=20)
enterdate = models.DateField()
class Meta:
db_table='t_scard'
def __unicode__(self):
return u'Scard:%s,%s'%(self.student.sname,self.major)
from post.models import *
stu = Student(sname='zhangsan')
stu.save()
scard = Scard(student=stu,department='计算机科学与技术系',major='计算机及应用',enterdate='2018-9-1')
scard.save()
#正向查询(主表的对象.从表类名小写)
Student.objects.first().scard
<Scard: Scard:zhangsan,计算机>
#逆向查询(从表的对象.一对一属性名)
Scard.objects.first().sno
<Student: zhangsan>
一对多的关系,有个关键字forognkey 1:n 班级 :学生、
动不动就封装函数,仍异常。
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
#班级 和学生 1:n
class Clazz(models.Model):
cno = models.AutoField(primary_key=True)
cname = models.CharField(max_length=30)
class Meta:
db_table='t_clazz'
def __unicode__(self):
return u'Clazz:%s--%s'%(self.cname)
# Create your models here.
# 学生 和 学生证的关系 1:1
class Student(models.Model):
#学号
sno = models.AutoField(primary_key=True)
#学生姓名
sname =models.CharField(max_length=30,unique=True)
#所在班级
clazz = models.ForeignKey(Clazz,related_name='student')
class Meta:
db_table='t_student'
def __unicode__(self):
return u'%s'%self.sname
from post.models import *
cls = Clazz(cname='B208Python班')
cls.save()
stu = Student(sname='lisi',clazz=cls)
stu.save()
Clazz.objects.first().student.all()
<QuerySet [<Student: zhangsan>]>
Student.objects.first().clazz.cname
u'B207Python'
封装函数插入数据
from stu.models import *
def insertData(clsname,*snames):
try:
cls = Clazz.objects.get(cname=clsname)
except Clazz.DoesNotExist:
cls = Clazz.objects.create(cname=clsname)
for sn in snames:
try:
stu = Student.objects.get(sname=sn)
except Student.DoesNotExist:
Student.objects.create(sname=sn,clazz=cls)
insertData('B202Python班','zhangjie','xiena')
多对多的关系
# 课程表 和 教师表 n:n
class Course(models.Model):
course_id=models.AutoField(primary_key=True)
course_name=models.CharField(max_length=30,unique=True)
class Meta:
db_table='t_course'
def __unicode__(self):
return u'Course:%s'%self.course_name
class Teacher(models.Model):
tid = models.AutoField(primary_key=True)
tname = models.CharField(max_length=30,unique=True)
course = models.ManyToManyField(Course)
class Meta:
db_table='t_teacher'
def __unicode__(self):
return u'Teacher:%s--%s'%(self.tname,self.course)
插入数据
from post.models import *
cour1 = Course(course_name='Java')
cour1.save()
cour2 = Course(course_name='Python')
cour2.save()
t1 = Teacher(tid='1001',tname='杨老师')
t1.save()
t2 = Teacher(tname='李老师')
t2.save()
t3 = Teacher(tname='张老师')
t3.save()
t1.course.add(cour1)
t2.course.add(cour2)
t3.course.add(cour1,cour2)
正向查询——逆向查询
Course.objects.first().teacher_set.all()
<QuerySet [<Teacher: Teacher:杨老师--post.Course.None>, <Teacher: Teacher:张老师--post.Course.None>]>
Teacher.objects.first().course.all()
<QuerySet [<Course: Course:Java>]>
封装函数插入数据
def insertData(tname,*coursenames): (可变参数)
cour_name=[]
for c in coursenames:
try:
cour = Course.objects.get(course_name=c)
except Course.DoesNotExist:
cour = Course.objects.create(course_name=c)
cour_name.append(cour)
t = Teacher(tname=tname)
t.save()
t.course.add(*cour_name) (解析参数)
insertData('wanglaoshi','语文','数学')