• django模型一对一关系


    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>')
  • 相关阅读:
    k8s-pv
    k8s ---kubectl 部署时,pull image 报错,拉取不到镜像
    【knowledgebase】不要在一个很大的RDD上调用collect
    【knowledgebase】如何知道partition数
    Spark SQL External Data Sources JDBC官方实现写测试
    Spark SQL External Data Sources JDBC官方实现读测试
    Sqoop2入门之导入关系型数据库数据到HDFS上(sqoop2-1.99.4版本)
    Spark Streaming、Kafka结合Spark JDBC External DataSouces处理案例
    Spark Streaming、HDFS结合Spark JDBC External DataSouces处理案例
    Spark SQL External Data Sources JDBC简易实现
  • 原文地址:https://www.cnblogs.com/glz666/p/13757098.html
Copyright © 2020-2023  润新知