django入门到精通⑦python对mysql,redis,mongodb的基本操作
# 创建项目、app
(python37_django2) D:\python\django_imooc_xiaobai>django-admin startproject lession4_orm_one
(python37_django2) D:\python\django_imooc_xiaobai>cd lession4_orm_one
(python37_django2) D:\python\django_imooc_xiaobai\lession4_orm_one>python manage.py startapp app
# 安装驱动
(python37_django2) D:\python\django_imooc_xiaobai\lession4_orm_one>pip install mysqlclient
# 执行对modoles的改动
(python37_django2) D:\python\django_imooc_xiaobai\lession4_orm_one>python manage.py makemigrations
Migrations for 'app':
app\migrations\0001_initial.py
- Create model Test
# 将models中的改动变更到数据库中
(python37_django2) D:\python\django_imooc_xiaobai\lession4_orm_one>python manage.py migrate
# 去掉表中的配置id,免得每次都需要定义id
# a.删除已经创建的数据库,并重新创建
MariaDB [lession4]> drop database lession4;
MariaDB [(none)]> create database lession4;
b.根据models中的定义,重新生成表结构
(python37_django2) D:\python\django_imooc_xiaobai\lession4_orm_one>python manage.py makemigrations
(python37_django2) D:\python\django_imooc_xiaobai\lession4_orm_one>python manage.py migrate
# 测试基本的增删改查
python manage.py shell
In [1]: from app.models import User
In [2]: user = User.objects.create(username='tom', age=33, phone=13866666688, email='tom@163.com', info='这家伙很懒')
In [3]: user
Out[3]: <User: user:tom>
In [4]: user = User(username='xiaoming', age=18, phone=155566668888, email='xiaoming@163.com', info='xiaoming info')
In [5]: user
Out[5]: <User: user:xiaoming>
In [6]: user.id
In [7]: user.age = 18
In [8]: user.save()
In [10]: user.age
Out[10]: 18
# 基本的过滤条件查询
In [54]: User.objects.create(username='lily',age=21,phone=100, email='lily@163.com',info='lily info')
Out[54]: <User: user:lily>
In [55]: User.objects.create(username='lucy',age=20,phone=101, email='lucy@163.com',info='lucy info')
Out[55]: <User: user:lucy>
In [56]: User.objects.create(username='hanmeimei',age=22,phone=102, email='hanmeimei@163.com',info='hanmeimei info')
Out[56]: <User: user:hanmeimei>
In [57]: User.objects.create(username='lilei',age=23,phone=103, email='lilei@163.com',info='lilei info')
Out[57]: <User: user:lilei>
# id>2 的所有用户
In [58]: User.objects.filter(id__gt=2)
Out[58]: <QuerySet [<User: user:xiaoqiang>, <User: user:lily>, <User: user:lucy>, <User: user:hanmeimei>, <User: user:lilei>]>
# startswith 用户名以l开头的用户
In [60]: User.objects.filter(username__startswith='l')
Out[60]: <QuerySet [<User: user:lily>, <User: user:lucy>, <User: user:lilei>]>
# 将对象转换成成字典
In [12]: vars
Out[12]: <function vars>
In [13]: _user = vars(user)
In [14]: _user
Out[14]:
{'_state': <django.db.models.base.ModelState at 0x18f687585f8>,
'id': 3,
'username': 'xiaoming',
'age': 18,
'phone': 155566668888,
'email': 'xiaoming@163.com',
'info': 'xiaoming info',
'create_time': datetime.datetime(2021, 1, 5, 8, 14, 6, 937307, tzinfo=<UTC>),
'update_time': datetime.datetime(2021, 1, 5, 8, 14, 6, 937307, tzinfo=<UTC>)}
# 多对多添加用户
In [1]: from app.models import UserProfile,Diary,Group,User
In [2]: import time
In [3]: user = User.objects.get(id=1)
In [4]: group = Group(name='运营', create_time=time.time())
In [5]: group.save()
In [6]: group.user.add(user)
In [10]: user = User.objects.create(username='xiaoqiang', age=18, phone=13666668888,email='xiaoq@163.com', info='xq info')
In [11]: group.user.add(user)
# 添加一个开发组,并将id为1的用户加入这个组
In [19]: group = Group.objects.create(name='开发', create_time=time.time())
In [20]: group.save()
In [21]: vars(group)
Out[21]:
{'_state': <django.db.models.base.ModelState at 0x2a873290da0>,
'id': 3,
'name': '开发',
'create_time': 1609850343.7919307}
In [22]: user = User.objects.get(id=1)
In [23]: group.user.add(user)
# 关联查询
In [25]: from app.models import User,Diary,UserProfile,Group
In [26]: jack = User.objects.get(pk=1)
In [27]: jack
Out[27]: <User: user:jack>
In [28]: jack.diary.values('content')
Out[28]: <QuerySet [{'content': '今天有事晴天'}]>
# 排除年龄小于20岁的人
In [46]: users = User.objects.all().exclude(age__lt=20)
In [47]: users
Out[47]: <QuerySet [<User: user:jack>, <User: user:tom>]>
# 找出用户名包括 ha 的用户
In [48]: users = User.objects.filter(username__contains='ja')
In [49]: users
Out[49]: <QuerySet [<User: user:jack>]>
# 关联查询
In [4]: from app.models import User,Diary,UserProfile,Group
In [5]: jack = users[0]
In [6]: jack
Out[6]: <User: user:jack>
In [9]: groups = jack.group
In [13]: groups.values('name')
Out[13]: <QuerySet [{'name': '运营'}, {'name': '开发'}]>
# 自定义sql获取数据
In [18]: users = User.objects.raw('select * from app_user')
In [19]: users
Out[19]: <RawQuerySet: select * from app_user>
In [20]: list(users)
Out[20]: [<User: user:jack>, <User: user:tom>, <User: user:xiaoqiang>]
# 聚合函数
In [29]: user = User.objects.aggregate(Avg('age'))
In [31]: user
Out[31]: {'age__avg': 28.0}
# sqlalchemy 数据库驱动
(python37_django2) D:\python\django_imooc_xiaobai\lession4_orm_one>pip install sqlalchemy
(python37_django2) D:\python\django_imooc_xiaobai\lession4_orm_one>pip install pymysql
(python37_django2) D:\python\django_imooc_xiaobai\lession4_orm_one>pip uninstall mysqlclient
MariaDB [(none)]> create database sqlalchemy_test;
# 插入数据
In [5]: from sqlalchemy_test import db_session,User
In [6]: user=User(name="jack002")
In [7]: db_session.add(user)
In [8]: db_session.commit()
In [9]: db_session.close()
# 查询
In [19]: jack = db_session.query(User).filter_by(name='jack001').one()
In [20]: jack.name
Out[20]: 'jack001'
redis缓存的使用
# 安装redis的相关依赖
(python37_django2) D:\python\django_imooc_xiaobai\lession4_orm_one\app>pip install redis
(python37_django2) D:\python\django_imooc_xiaobai\lession4_orm_one\app>pip install django-redis
settings.py添加redis缓存的配置
CACHES = {
'default': {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': 'redis://127.0.0.1:6379',
'OPTIONS': {
'CLIENT_CLASS': 'django_redis.client.DefaultClient',
'CONNECTION_POOL_KWARGS': {'max_connections': 200}
# 'PASSWORD': 'XXX'
}
}
}
# 要安装redis 5,否则可能不支持过期时间的设置
windows版本redis下载:https://github.com/tporadowski/redis/releases/download/v5.0.10/Redis-x64-5.0.10.msi
In [1]: from django_redis import get_redis_connection
In [2]: cache = get_redis_connection('default')
In [3]: cache.set('name','jack',30)
Out[3]: True
In [4]: cache.get('name')
Out[4]: b'jack'
原生redis
In [5]: import redis
In [6]: cache = redis.Redis(host='localhost', port=6379)
In [7]: cache
Out[7]: Redis<ConnectionPool<Connection<host=localhost,port=6379,db=0>>>
In [8]: cache.get('name')
In [9]: cache.set('name','jack01')
Out[9]: True
In [10]: cache.get('name')
Out[10]: b'jack01'
In [11]: value = cache.get('name')
In [12]: type(value)
Out[12]: bytes
In [13]: import json
In [14]: data = {'age':20}
In [15]: cache.set('name', json.dumps(data))
Out[15]: True
In [16]: value=cache.get('name')
In [17]: value
Out[17]: b'{"age": 20}'
In [18]: json.loads(value)
Out[18]: {'age': 20}
In [19]: value.decode('utf-8')
Out[19]: '{"age": 20}'
In [20]: json.loads(value.decode('utf-8'))
Out[20]: {'age': 20}
在models中包装redis获取用户的方法
# encoding:utf-8
import json
from functools import wraps
from django.db import models
from django_redis import get_redis_connection
_cache = get_redis_connection('default')
def cache(func):
@wraps(func)
def wrapper(obj, *args):
key = args[0]
value = _cache.get(key)
if value:
return json.loads(value)
rs = func(obj, *args)
_cache.set(key, json.dumps(rs))
return rs
return wrapper
class Test(models.Model):
name = models.IntegerField()
class User(models.Model):
# id = models.IntegerField(primary_key=True)
username = models.CharField(unique=True, max_length=50, blank=False)
age = models.SmallIntegerField(default=0)
phone = models.IntegerField(db_index=True, blank=True, default=0)
email = models.EmailField(blank=True, default='')
info = models.TextField()
create_time = models.DateTimeField(auto_now_add=True)
update_time = models.DateTimeField(auto_now=True)
# 联合索引
class Meta:
index_together = ['username', 'phone']
# 定义返回默认信息
def __str__(self):
return 'user:{}'.format(self.username)
@classmethod
@cache
def get(cls, id):
rs = cls.objects.get(id=id)
return {
'id': rs.id,
'username': rs.username,
'age': rs.age,
'email': rs.email,
'info': rs.info,
'create_time': str(rs.create_time),
'update_time': str(rs.update_time)
}
class UserProfile(models.Model):
# id = models.IntegerField(primary_key=True)
# 一对一关系表
user = models.OneToOneField(User, blank=True, default='', null=True, on_delete=models.SET_NULL)
birthday = models.CharField(max_length=100, blank=True, default='')
def __str__(self):
return 'user:{}, birthday: {}'.format(self.user.username, self.birthday)
class Diary(models.Model):
# 一对多的表关系,人员和日记
# id = models.IntegerField(primary_key=True)
user = models.ForeignKey(User, related_name='diary', on_delete=models.SET_NULL, blank=True, null=True)
content = models.TextField()
create_time = models.IntegerField()
class Group(models.Model):
# 多对多,一个员工是多方面的能手,可能同时在产品组和开发组,一个组有多个员工
# id = models.IntegerField(primary_key=True)
user = models.ManyToManyField(User, related_name='group')
name = models.CharField(max_length=20)
create_time = models.IntegerField()
# 测试
In [1]: from app.models import User
In [2]: user = User.get(1)
In [3]: user
Out[3]:
{'id': 1,
'username': 'jack007',
'age': 33,
'email': 'jack@163.com',
'info': '这家伙很懒',
'create_time': '2021-01-05 07:56:23.953842+00:00',
'update_time': '2021-01-06 01:30:59.049096+00:00'}
mongodb在django中的使用
服务端安装包下载地址:https://www.mongodb.com/try/download/community
# 安装pymongo驱动
(python37_django2) C:\Users\ws>pip install pymongo
命令行简单操作mongodb数据库
直接运行客户端就可以进入命令行shell
D:\mongodb4.0.22\bin\mongo.exe
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> use test
switched to db test
> use test_jack
switched to db test_jack
> db.user.insert({'name':'jack'})
WriteResult({ "nInserted" : 1 })
>
> db.user.insert({'_id':'fadfjljl341jl', 'name':'xiaoming'})
WriteResult({ "nInserted" : 1 })
> db.user.find()
{ "_id" : ObjectId("5ff861ffa08d201119e75650"), "name" : "jack" }
{ "_id" : "fadfjljl341jl", "name" : "xiaoming" }
ipython环境操作mongodb
创建操作mongo的models
app/mongo_models.py
# _*_ coding:utf-8 _*_
# __author__ == 'jack'
# __date__ == '2021-01-08 10:03 PM'
from django.conf import settings
conn = settings.MONGOCLIENT['test_mongo']
class User(object):
db = conn['user']
@classmethod
def insert(cls, **params):
return cls.db.insert(params)
@classmethod
def get(cls, **params):
return cls.db.find(params)
# 通过shell操作
python manage.py shell
In [1]: from app.mongo_models import User
In [2]: data = {'_id':'001','name':'jack','age':33}
In [3]: result = User.insert(**data)
In [4]: result
Out[4]: '001'
In [5]: data = User.get(_id=result)
In [6]: list(data)
Out[6]: [{'_id': '001', 'name': 'jack', 'age': 33}]
In [14]: User.insert(_id='adfa',name='tom',age=20)
Out[14]: 'adfa'
In [4]: User.update(id='adfa',name='tom001')
Out[4]: {'n': 1, 'nModified': 1, 'ok': 1.0, 'updatedExisting': True}
In [5]: rs = User.get(_id='adfa')
In [6]: rs
Out[6]: {'_id': 'adfa', 'name': 'tom001', 'age': 20}
In [9]: from bson import ObjectId
In [10]: rs = User.get(name='xiaoming')
In [11]: rs
Out[11]: {'_id': ObjectId('5ffef06328410c4b4416fb57'), 'name': 'xiaoming', 'age': 30}
In [12]: str(rs.get('_id'))
Out[12]: '5ffef06328410c4b4416fb57'
In [13]: ObjectId('5ffef06328410c4b4416fb57')
Out[13]: ObjectId('5ffef06328410c4b4416fb57')
In [14]: import uuid
In [16]: uuid.uuid4()
Out[16]: UUID('146012fc-f403-429e-b3bf-ba889022ff2e')
In [17]: '{}'.format(uuid.uuid4())
Out[17]: '4bd5acb4-143b-4ee8-86d1-3203b139831c'
In [18]: import time
In [19]: '{}{}'.format(uuid.uuid4(),time.time())
Out[19]: 'dca2708f-a015-4a17-b3a1-af08967e407b1610543348.1435869'
# mongo的表关联
(python37_django2) D:\python\django_imooc_xiaobai\lession4_orm_one>pip install mongoengine
app/mongo_engine.py
# _*_ coding:utf-8 _*_
# __author__ == 'jack'
# __date__ == '2021-01-13 9:10 PM'
from mongoengine import connect, Document
from mongoengine import StringField, IntField, ReferenceField
connect('test_mongo', host='localhost', port=27017)
class Users(Document):
name = StringField(required=True, max_length=200)
age = IntField(required=True)
class Paper(Document):
title = StringField(required=True, max_length=200)
user = ReferenceField(Users)
# 测试
In [2]: from app.mongo_engine import Users,Paper
In [3]: user = Users.objects.create(name='jack', age=33)
In [4]: user
Out[4]: <Users: Users object>
In [5]: user.age
Out[5]: 33
In [6]: user.id
Out[6]: ObjectId('5ffef269cf8f5cc391ed4635')
In [7]: paper = Paper(title='test paper',user=user)
In [8]: paper.title
Out[8]: 'test paper'
In [9]: paper.user.name
Out[9]: 'jack'
In [13]: paper.save()
Out[13]: <Paper: Paper object>
In [14]: rs = Paper.objects.filter(title='test paper')
In [15]: rs
Out[15]: [<Paper: Paper object>]
In [16]: rs = Paper.objects.get(title='test paper')
In [17]: rs.user
Out[17]: <Users: Users object>