hello world

人生若只如初见

Django(3)--关联mysql

ORM

对象关系映射,是为了解决面向那个对象与数据库存在的互不匹配的现象的技术。

mysql创建数据库

  • 本地安装mysql,并创建数据库
create databases bookmanager;
  • settings.py
#连接本地数据库,不能把settings.py代码暴露在公网中
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'bookmanager',
        'USER': 'root',
        'PASSWORD': 'mysql',
        'HOST':'192.168.234.133',
        'PORT':'3306',
    }
}
  • 放入settings.py同级目录__init__.py
import pymysql  ##连接mysql
pymysql.install_as_MySQLdb()
  • app01/models.py 放表(创建表)
    • 类---》表
    • 对象---》数据行(记录)
    • 属性---》字段
#定义表格式
from django.db import models
class User(models.Model): #继承模块
    username = models.CharField(max_length=32) #varchar(32)
    password = models.CharField(max_length=32
app01\migrations\000PS E:\python_data\py27\bookmanager> python manage.py migrate  #创建表结构
Operations to perform:
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... 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... OK1_initial.py
    - Create model User
PS E:\python_data\py27\bookmanager> python manage.py makemigrations app01 #让 Django 知道我们在我们的模型有一些变更,做迁移文件
Migrations for 'app01':
  app01\migrations\0001_initial.py
    - Create model User
PS E:\python_data\py27\bookmanager> python manage.py migrate app01 # 创建表结构,将表迁移到数据库
Operations to perform:
  Apply all migrations: app01
Running migrations:
  Applying app01.0001_initial... OK
  • 打开数据库视图

数据库类

  • views.py
from django.shortcuts import render,HttpResponse,redirect
from  app01 import models

def index(request):
    # 获取所有数据
    ret = models.User.objects.all() #固定写法,QuerySet 对象列表
    print(type(ret),ret)
    return render(request,'index.html')
  • 访问index页面
System check identified no issues (0 silenced).
November 20, 2019 - 00:04:16
Django version 1.11.25, using settings 'bookmanager.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Not Found: /
[20/Nov/2019 00:04:18] "GET / HTTP/1.1" 404 2129
<class 'django.db.models.query.QuerySet'> <QuerySet [<User: User object>, <User: User object>]>
[20/Nov/2019 00:04:27] "GET /index/ HTTP/1.1" 200 157

#一个User对象对应一条数据
  • views.py 循环返回的列表
def index(request):
    # 获取所有数据
    ret = models.User.objects.all() #固定写法
    print(type(ret),ret)
    for i in ret:
        print(i.username,i.password,type(i.username))
    ret = models.User.objects.get(username='alex',password='123') #获取单条指定数据
    print(ret,type(ret))
    return render(request,'index.html')
  • 访问index页面

System check identified no issues (0 silenced).
November 20, 2019 - 00:09:59
Django version 1.11.25, using settings 'bookmanager.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
<class 'django.db.models.query.QuerySet'> <QuerySet [<User: User object>, <User: User object>]>
xiaoming 123.com <class 'str'> #拿到数据
alex 123 <class 'str'>
[20/Nov/2019 00:10:03] "GET /index/ HTTP/1.1" 200 157

posted on 2019-11-20 00:18  一壶兔  阅读(204)  评论(0编辑  收藏  举报

导航