博客
关于我
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/

你可能感兴趣的文章
Logback 配置文件例
查看>>
logback-spring.xml配置
查看>>
logback.xml 配置详解(1)
查看>>
logback.xml配置
查看>>
logback.xml配置导入spring无法启动:ch.qos.logback.core.joran.spi.JoranException: I/O error occurred while par
查看>>
logback异步日志AsyncAppender配置
查看>>
logback的使用和logback.xml详解
查看>>
logback配置
查看>>
logback配置文件详解
查看>>
Logback配置输出sql
查看>>
logger4j 日志配置内,各种符号详解
查看>>
logging.config报错FileNotFoundError
查看>>
Logistic回归Cost函数和J(θ)的推导(二)----梯度下降算法求解最小值
查看>>
Logistic回归梯度下降
查看>>
logistic回归模型python_【机器学习速成宝典】模型篇03逻辑斯谛回归【Logistic回归】(Python版)...
查看>>
logo下方显示技术支持信息_关于TNB名称、信息被非法盗用的严正声明
查看>>
logrotate切割日志rsyslog不重新上传,清空归零iCurrOffs
查看>>
logrotate的日志轮转
查看>>
Logstash input jdbc连接数据库
查看>>
logstash mysql 准实时同步到 elasticsearch
查看>>