django模型一对一关系
模型:创建顾客信息表customer和顾客详细信息表customerinfo
from django.db import models # Create your models here. #一对一关系 # 顾客表 class Customer(models.Model): name=models.CharField(max_length=10) class Meta(): db_table = 'customer' # 顾客详细信息表 class CustomerInfo(models.Model): # 在顾客详细信息表中关联用户表,让两个表的数据产生联系 cid = models.OneToOneField(Customer, on_delete=models.CASCADE) # 第一个参数:是被关联的模型名称 # 第二个参数:当被关联的顾客customer表中的一条数据被删除的时候,与之关联的顾客详细信息customerinfo表中的数据也会被删除 address = models.CharField(max_length=50) class Meta(): db_table = 'customerinfo'
根路由
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('app/', include('myapp.urls')), ]
子路由
from django.urls import path, re_path from . import views urlpatterns = [ path('oto_add_01/', views.onetoone_add_01), path('oto_add_02/', views.onetoone_add_02), path('oto_search/', views.onetoone_search), path('oto_update/', views.onetoone_update), path('oto_delete/', views.onetoone_delete), ]
视图函数
from django.shortcuts import render from django.http import HttpResponse, Http404 from django.urls import reverse from . import models # 增 def onetoone_add_01(request): # 1.在添加顾客信息时添加顾客详细信息 add_customer = models.Customer() add_customer.name = 'jack' add_customer.save() # 添加顾客详情表数据 add_customerinfo = models.CustomerInfo() add_customerinfo.address = 'shanghai' # 关联顾客表,关联的是顾客表对象 add_customerinfo.cid = add_customer add_customerinfo.save() return HttpResponse('<script>alert("success");</script>') def onetoone_add_02(rquest): # 2.在已有顾客信息后添加顾客详细信息 # 获取需要添加详细信息的顾客 customer = models.Customer.objects.all().get(name='pony') print(customer) add_customerinfo = models.CustomerInfo() add_customerinfo.address = 'chengdu' # 关联的是顾客表对象 add_customerinfo.cid = customer add_customerinfo.save() return HttpResponse('<script>alert("success");</script>') # 查 def onetoone_search(request): # 1.通过顾客表customer查找顾客详细信息表数据 customerobj = models.Customer.objects.filter(name='pony').first() print(customerobj.name) # 根据顾客表customer查找顾客详细信息表customerinfo数据: 顾客对象.顾客详细信息类名(小写).顾客详细信息属性 print(customerobj.customerinfo.address) # 2.通过顾客详细信息表customerinfo查找顾客表数据 customerinfoobj = models.CustomerInfo.objects.first() print(customerinfoobj.address) # 根据顾客详细信息表customerinfo查找顾客表customer数据: 顾客详细信息对象.外键.顾客信息属性 print(customerinfoobj.cid.name) return HttpResponse('<script>alert("success");</script>') # 改 def onetoone_update(request): customerobj = models.Customer.objects.filter(name='pony').first() customerobj.name = 'zhuge' customerobj.customerinfo.address = 'xixia' customerobj.save() customerinfoobj = models.CustomerInfo.objects.filter(address='beijing').first() customerinfoobj.address = 'lechang' customerinfoobj.cid.name = 'kunge' customerinfoobj.save() return HttpResponse('<script>alert("success");</script>') # 删 def onetoone_delete(request): # 1.删除顾客信息表customer数据,与子关联的顾客详细信息表customerinfo的数据也会删除 customerobj = models.Customer.objects.filter(id=4).first() customerobj.delete() # 2.删除顾客详细信息表customerinfo数据,与子关联的顾客信息表customer的数据不会产生影响 customerinfoobj = models.CustomerInfo.objects.filter(id=9).first() customerinfoobj.delete() return HttpResponse('<script>alert("success");</script>')