在py3.*中利用django使用mysql时,会出现一些问题。由于django默认的是 MySQLdb,但MySQLdb目前不支持py3.*所以我们要改用pymysql,首先要安装pymysql,命令如下:
pip3 install pymysql
然后我们去项目同名的文件夹下找到setting.py中修改:
1 DATABASES = { 2 'default': { 3 'ENGINE': 'django.db.backends.mysql', 4 'NAME': "dbname", 5 "USER":"dbusername", 6 "PASSWORD":"dbpassword", 7 "HOST":"127.0.0.1", 8 "PORT":"3306", 9 } 10 }
最后 去项目同名的文件夹下找到 __init__.py文件并在其中添加如下代码:
import pymysql pymysql.install_as_MySQLdb()
然后执行:
python manage.py makemigrations
python manage.py migrate
做一个小的测试:
创建一个app
python manage.py startapp app02
由于想让app2管理app2下面所有的链接地址,而不是通过项目同名下的文件中的urls.py来管理那么我们需要这么做
项目同名文件夹中的urls.py
1 from django.contrib import admin 2 from django.urls import path,re_path,include 3 urlpatterns = [ 4 path('admin/', admin.site.urls), 5 path("cmdb/",include("app02.urls")), 6 ]
重点是path("cmdb/",include("app02.urls")) 然后我们在app02下创建一个urls.py
"""study_django01 URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/2.1/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin from django.urls import path,re_path from app02 import views urlpatterns = [ path("orm/",views.orm), ]
然后再app002下的models.py中编写如下代码
1 from django.db import models 2 3 # Create your models here. 4 5 # 必须继承models.Model 6 7 class UserInfo(models.Model): 8 # id列 自增 主键 系统默认会创建这样的玩意儿 9 username = models.CharField(max_length=32) 10 password = models.CharField(max_length=64)
在命令行执行
python manage.py makemigrations
python manage.py migrate
去数据库中查看对应的数据库,可以看到系统为您生成了app02_userinfo表,并且字段就是你填写的两个字段,需要注意的是,系统将会为你自动生成Id字段,并且是自增、主键。
最后在app002下的view.py中编写如下代码
1 from django.shortcuts import render,HttpResponse 2 3 # Create your views here. 4 from app02 import models 5 6 def orm(request): 7 # 增********************* 8 # 创建数据方式一 (推荐) 9 # models.UserInfo.objects.create( username="root", password="123456") 10 11 # 创建数据方式二 12 # obj = models.UserInfo(username="root",password="123456") 13 # obj.save() 14 15 # 创建数据方式三 16 # dic = {"username":"eric","password":"6666"} 17 # models.UserInfo.objects.create(**dic) 18 19 # 查********************** 20 # 查所有 21 result = models.UserInfo.objects.all() 22 print(result) 23 for row in result: 24 print(row.id,row.username,row.password) 25 26 # 按条件查 27 result1 = models.UserInfo.objects.filter(username="root",password="3") 28 print(result1) 29 30 # 删除 31 # 全部删除 32 # models.UserInfo.objects.all().delete() 33 34 # 按条件删除 35 models.UserInfo.objects.filter(id=4).delete() 36 37 # 更新 38 # 全部更新 39 models.UserInfo.objects.all().update(password=666) 40 # 指定更新 41 models.UserInfo.objects.filter(id=3).update(password=111) 42 43 return HttpResponse("orm")