16_创建多表
一对一关系
使用OneToOneField()
# -*- coding: utf-8 -*-
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 __str__(self): # python 3 对象输出的方法
return 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 __str__(self):
return (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>
一对多关系
# -*- coding: utf-8 -*-
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 __str__(self): # python 3 对象输出的方法
return self.cname
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') # related_name="": 设置反向查询时的属性名称
class Meta:
db_table='t_student'
def __str__(self): # python 3 对象输出的方法
return 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 __str__(self): # python 3 对象输出的方法
return 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 __str__(self): # python 3 对象输出的方法
return (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','语文','数学')
17_可变参数_参数解包
可变参数
- *args将收集所有位置相关的参数,并放到一个元组中,最后将这个元组赋值给args
- **kwargs指针对关键字参数(指a=3这一类型) 这些关键字参数会放到一个相应的字典中,然后同样的赋值给kwargs
def demo(*args,**kwargs):
print args,kwargs
demo()
"""
()
{}
"""
demo(1,2)
"""
(1,2)
{}
"""
demo(c='a',b='d')
"""
()
{'c': 'a', 'b': 'd'}
"""
demo(1,2,c='a',b='d')
"""
(1, 2)
{'c': 'a', 'b': 'd'}
"""
参数解包
1. *arg可以解包元组,而**kwarg则可以解包字典
def demo(a,b,c):
print a,b,c
kwargs = {'a':1,'b':2,'c':3}
demo(**kwargs)
结果:1 2 3
args =(1,2,3)
demo(*args)
1 2 3
可变参数和参数解包区别
- 可变参数是在形参中,利用*,**来实现
- 参数解包是在实参中,利用*,**来实现