模型关系
1:1 一对一 (一个身份证对应一个驾照)
- 是使用外键+唯一约束实现的
- 对应最多只能有一个
- 我们通常会在从表中声明关系
- 主表,从表
- 主表数据删除,从表数据级联删除
- 从表数据删除,主表不受影响
- 谁声明关系,谁就是从表
driver_idcard = models.OneToOneField(IDCard,on_delete=models.PROTECT)
- 主表获取从表数据,是通过隐性属性,隐性属性的名字 默认是从表名字小写
print(idcard.drivercard.driver_lever)
- 从表获取主表数据,是通过显性属性
print(dc.driver_idcard.id_name)
1:M(一个人对应多辆车)
- 使用外键实现的
- 主表获取从表是隐性属性,隐性属性是Manager的子类
- 支持
- all
- filter
- first
- last
- 支持
- 数据删除和一对一一样
- 从表获取主表数据,是通过显性属性idcard = car.c_idcardprint(idcard.id_name- 主表获取从表数据,是通过隐性属性,隐性属性的名字 默认是从表名字小写idcard = IDCard.objects.last()cars = idcard.car_set.last()
实例
models.py
from django.db import models# Create your models here.
class IDCard(models.Model):id_num = models.CharField(max_length=32,unique=True)id_name = models.CharField(max_length=32)class DriverCard(models.Model):diver_leverl = models.CharField(max_length=16)driver_idcard = models.OneToOneField(IDCard,on_delete=models.PROTECT)class Car(models.Model):c_type = models.CharField(max_length=32)c_idcard = models.ForeignKey(IDCard,null=True)
view.py
import random
import uuidfrom django.http import HttpResponse
from django.shortcuts import render# Create your views here.
from RelationLearn.models import IDCard, DriverCard, Cardef index(request):return HttpResponse("three index")def add_idcard(request):id_card = IDCard()id_card.id_num = random.randrange(10000000000)id_card.id_name = uuid.uuid4().hexid_card.save()return HttpResponse("add new id card ok")def add_dc(request):dc = DriverCard()dc.diver_level = "c1"id_card = IDCard.objects.last()dc.driver_idcard =id_carddc.save()return HttpResponse("add killer ok")def delete_idcard(request):id_card = IDCard.objects.last()id_card.delete()return HttpResponse("del idcard ok")def del_dc(request):dc_card = DriverCard.objects.last()dc_card.delete()return HttpResponse("del dc ok")def get_idcard(request):dc= DriverCard.objects.last()print(dc.driver_idcard.id_name)return HttpResponse("by carid get id")def get_dc(request):idcard= IDCard.objects.last()print(idcard.drivercard.driver_lever)return HttpResponse("by id get carid")def get_idcard_form_car(request):car =Car.objects.last()idcard = car.c_idcardprint(idcard.id_name)return HttpResponse("by car gei id ")def get_car(request):idcard = IDCard.objects.last()cars = idcard.car_set.last()for car in cars:print(car)car_single = idcard.car_set.first()print(car_single.c_type)return HttpResponse("get cars by id")def add_car(request):# car = Car()# car.c_type = "tiganche"# idcard = IDCard.objects.last()# car.c_idcard = idcard# car.save()car = Car()car.c_type = "olddriver"car.save()idcard = IDCard.objects.last()idcard.car_set.add(car)return HttpResponse("add car ")
- M:N
- 使用额外的关系表进行数据记录
- 关系表使用两个外键实现的
from django.db import models# Create your models here.
class BlogUser(models.Model):u_name =models.CharField(max_length=32)class Blog(models.Model):b_title =models.CharField(max_length=64)b_content =models.TextField()b_user_collect =models.ManyToManyField(BlogUser)
views.py
from django.http import HttpResponse
from django.shortcuts import render# Create your views here.
from ManyLearn.models import *def index(request):return HttpResponse("index many")def add_collect(request):# blog = Blog.objects.last()# # blog= Blog()# user =BlogUser.objects.first()# blog.b_user_collect.add(user)# return HttpResponse("add collect")blog = Blog.objects.last()user = BlogUser.objects.last()user.blog_set.add(blog)return HttpResponse("add collect")def del_collects(request):blog = Blog.objects.all()user = BlogUser.objects.get(id = "3")user.blog_set.remove(*blog)return HttpResponse("del collects ok")
# 查询指定用户的收藏的所有博客
def get_usercollects(request):user = BlogUser.objects.first()# print(user.blog_set.all().values())collections = user.blog_set.all()return render(request,"AllCollects.html",context=locals())
# 查询指定博客被哪些用户收藏了
def get_user(request):blog = Blog.objects.first()# blog=Blog()users = blog.b_user_collect.all()return render(request,"AllCollectsUser.html",context=locals())
模型级联操作
- CASECADE
- 默认模式
- 默认级联删除
- PROTECT
- 保护模式
- 存在级联数据就不能删除
- 不存在级联数据就可以删除
- SET
- 设置为某值
- NULL
- 字段允许为空
- DEFAULT
- 字段存在默认值
- VALUE
- VALUE是真实的值