django2.0演示mysql下的一对多增删查改操作

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

  • 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_name

class 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 makemigrations
Migrations for 'app01':
  app01\migrations\0001_initial.py
    - Create model Account
    - Create model Contact

C:\Users\Administrator\Desktop\核心文件\python_file\python尝试\平常练习django\test02(一对多关系)>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, app01, auth, contenttypes, sessions
Running 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 models

In [2]: a1 = models.Account.objects.create(user_name='Rose')

In [3]: c1 = models.Contact.objects.create(account=a1,mobile='123456')

In [4]: c1.save
Out[4]: <bound method Model.save of <Contact: Rose, 123456>>

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.account
Out[8]: <Account: Account: Rose>

In [9]: print(c1.account)
Account: Rose

In [10]: print(c2.account)
Account: Rose

In [11]: c3 = models.Contact.objects.filter(pk=1)

In [12]: c3
Out[12]: <QuerySet [<Contact: Rose, 123456>]>


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


In [13]: c2.mobile = '78910'

In [14]: c2.save()

In [15]: c2
Out[15]: <Contact: Rose, 78910>

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]: <QuerySet []>

In [19]: 

你可能感兴趣的:(django,django,python,mysql,orm)