博客
关于我
django2.0演示mysql下的一对多增删查改操作
阅读量:143 次
发布时间:2019-02-28

本文共 3264 字,大约阅读时间需要 10 分钟。

本博文源于django基础操作,旨在演示django下orm对mysql一对多关系模型的基础操作。完成本实验前,可参考此博文的基础配置可参考此博文,简单易操作。

里面包含:

  • django运行成功
  • 资源路径配置,链接App,注释csrf的操作

其中资源路径可不配置,外加链接mysql数据库。

DATABASES = {       'default': {           'ENGINE':'django.db.backends.mysql', # 统一规定        'NAME':'test', # 需要自己创建数据库,然后将名字写在上面        'HOST':'127.0.0.1', # 一般为固定套路        'PORT':3306, # 固定套路        'USER':'root', # 自身的user        'PASSWORD':'123456', # 自身的password    }}

下面开始本实验。

实验步骤

  • 在models里码两张表的创建
  • 命令行生成数据库,及迁移文件
  • 命令行下操作mysql数据表,进行增删查改!

models.py创建账户和联系表

from django.db import models# Create your models here.class Account(models.Model):    user_name = models.CharField(max_length=80)    password = models.CharField(max_length= 255)    def __str__(self):        return "Account: %s"%self.user_nameclass Contact(models.Model):    account = models.ForeignKey(        Account,        on_delete=models.CASCADE,    )    mobile = models.CharField(max_length=20)    def __str__(self):        return "%s, %s"%(self.account.user_name,self.mobile)

生成数据库迁移

soft Windows [版本 10.0.18362.145](c) 2019 Microsoft Corporation。保留所有权利。C:\Users\Administrator\Desktop\核心文件\python_file\python尝试\平常练习django\test02(一对多关系)>python manage.py makemigrationsMigrations for 'app01':  app01\migrations\0001_initial.py    - Create model Account    - Create model ContactC:\Users\Administrator\Desktop\核心文件\python_file\python尝试\平常练习django\test02(一对多关系)>python manage.py migrateOperations to perform:  Apply all migrations: admin, app01, auth, contenttypes, sessionsRunning migrations:  Applying contenttypes.0001_initial... OK  Applying auth.0001_initial... OK  Applying admin.0001_initial... OK  Applying admin.0002_logentry_remove_auto_add... OK  Applying admin.0003_logentry_add_action_flag_choices... OK  Applying app01.0001_initial... OK  Applying contenttypes.0002_remove_content_type_name... OK  Applying auth.0002_alter_permission_name_max_length... OK  Applying auth.0003_alter_user_email_max_length... OK  Applying auth.0004_alter_user_username_opts... OK  Applying auth.0005_alter_user_last_login_null... OK  Applying auth.0006_require_contenttypes_0002... OK  Applying auth.0007_alter_validators_add_error_messages... OK  Applying auth.0008_alter_user_username_max_length... OK  Applying auth.0009_alter_user_last_name_max_length... OK  Applying auth.0010_alter_group_name_max_length... OK  Applying auth.0011_update_proxy_permissions... OK  Applying sessions.0001_initial... OK

对数据库进行orm操作

增加两张数据表数据字段操作

In [1]: from app01 import modelsIn [2]: a1 = models.Account.objects.create(user_name='Rose')In [3]: c1 = models.Contact.objects.create(account=a1,mobile='123456')In [4]: c1.saveOut[4]: 
>In [5]: c1.save()In [6]: c2 = models.Contact.objects.create(account=a1,mobile='654321')In [7]: c2.save()In [8]:

查询相关数据表记录(查询c1,c2的信息,过滤信息)

In [8]: c1.accountOut[8]: 
In [9]: print(c1.account)Account: RoseIn [10]: print(c2.account)Account: RoseIn [11]: c3 = models.Contact.objects.filter(pk=1)In [12]: c3Out[12]:
]>

修改Contact数据表记录(将c2的phone修改)

In [13]: c2.mobile = '78910'In [14]: c2.save()In [15]: c2Out[15]: 
In [16]:

删除Account表的相关记录(查看是否级联删除)

In [16]: a1.delete()Out[16]: (3, {   'app01.Contact': 2, 'app01.Account': 1})In [17]: a1.save()In [18]: models.Contact.objects.all()Out[18]: 
In [19]:

转载地址:http://swgd.baihongyu.com/

你可能感兴趣的文章
Nacos心跳机制实现快速上下线
查看>>
nacos报错com.alibaba.nacos.shaded.io.grpc.StatusRuntimeException: UNAVAILABLE: io exception
查看>>
nacos服务提供和发现及客户端负载均衡配置
查看>>
Nacos服务注册与发现demo
查看>>
Nacos服务注册与发现的2种实现方法!
查看>>
nacos服务注册和发现原理简单实现案例
查看>>
Nacos服务注册总流程(源码分析)
查看>>
nacos服务注册流程
查看>>
Nacos服务部署安装
查看>>
nacos本地可以,上服务器报错
查看>>
Nacos注册Dubbo(2.7.x)以及namespace配置
查看>>
Nacos注册中心有几种调用方式?
查看>>
nacos注册失败,Feign调用失败,feign无法注入成我们的bean对象
查看>>
nacos源码 nacos注册中心1.4.x 源码 nacos源码如何下载 nacos 客户端源码下载地址 nacos discovery下载地址(一)
查看>>
nacos源码 nacos注册中心1.4.x 源码 spring cloud alibaba 的discovery做了什么 nacos客户端是如何启动的(二)
查看>>
nacos源码 nacos注册中心1.4.x 源码 如何注册服务 发送请求,nacos clinet客户端心跳 nacos 注册中心客户端如何发送的心跳 (三)
查看>>
Nacos源码分析:心跳机制、健康检查、服务发现、AP集群
查看>>
nacos看这一篇文章就够了
查看>>
Nacos简介、下载与配置持久化到Mysql
查看>>
Nacos简介和控制台服务安装
查看>>