=================================================================================
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app_mysql.apps.AppMysqlConfig', # 这个是我们pycharm创建时候自动帮我们注册的就是app配置
'app_mysql', # 如果有新的或者命令行创建的app我们只要这这里面把app名称写上去即可
]
DATABASES = {
'default':{
'ENGINE':'django.db.backends.sqlite3', #sqlite3数据库是个小型的数据库
'NAME':os.path.join(BASE_DIR,'sqlite3') #NAME填写路径即可
}
# 'default': {
# 'ENGINE': 'django.db.backends.mysql',
# 'NAME':'库名',
# 'USER':'mysql账号名称',
# 'PASSWORD':'mysql密码',
# 'HOST':'127.0.0.1',
# }
}
import pymysql
pymysql.install_as_MySQLdb()
from django.db import models
# Create your models here.
class sb(models.Model):
# 主键自增id不用写, 默认会加上
name = models.CharField(max_length=30,null=True)
class big_sb(models.Model):
name = models.CharField(max_length=30, null=True)
bigsb = models.ForeignKey('sb',id)
class sb2(models.Model):
name = models.CharField(max_length=30,null=True)
python manage.py makemigrations
python manmge.py migrate
=================================================================================
注意: 必须先在逻辑业务层中导入
from 表单所在的app名称 import models
# 其中表名均为在models中配置的表的名称
1.1 单条数据:
1.2 多条数据:
info = [
models.UserInfo(name='root1', age=34, ut_id=1),
models.UserInfo(name='root2', age=35, ut_id=2),
models.UserInfo(name='root3', age=36, ut_id=1),
models.UserInfo(name='root4', age=37, ut_id=3),
models.UserInfo(name='root5', age=32, ut_id=1),
]
models.UserInfo.objects.bulk_create(info)
models.表名.objects.filter(满足的条件).delete()
models.表名.objects.filter(满足的条件).update(name='lll', age=23)
models.UserType.objects.all().values()
models.A.objects.all().values('ud__id')
res = models.A.objects.all()
for a in res:
print(a.ud.id)
models.B.objects.all().values('A__id')
res = models.B.objects.all()
for b in res:
print(b.a_set.id) #### 表名小写_set
=================================================================================
filter: 满足条件的
exclude: 不满足条件
#id等于3的
models.表名.objects.filter(id=3).values()
#id不等于3的
models.表名.objects.exclude(id=3).values()
关于filter与exclude里面填写的参数:
res = models.表名.objects.filter(字段名__in=[2,4,5]) # where id in (2,4,5)
res = models.表名.objects.exclude(字段名__in=[1,2]) # where id not in (1,2)
# where id between 4 and 8 [4,8]
res = models.表名.objects.filter(字段名__range=[4,8])
# where name like 'a%'
res = models.表名.objects.filter(字段名__startswith="a")
res = models.表名.objects.filter(字段名__istartswith="a") #忽略大小写
# where name like '%a'
res = models.表名.objects.filter(字段名__endswith="a")
res = models.表名.objects.filter(字段名__iendswith="a") #忽略大小写
# where name like '%a%'
res = models.表名.objects.filter(字段名__contains="a")
res = models.表名.objects.filter(字段名__icontains="a") #忽略大小写
只要是i开头的基本上都是忽略大小写
# select count(*) from userinfo where id>3;
# select count(id) from userinfo where id>3;
#用sql语句查询数据条数尽量不要查count(*)查主键会快很多
res = models.UserInfo.objects.filter(id__gt=3).count()
#升序
res = models.表名.objects.order_by('字段名称')
#降序
res = models.表名.objects.order_by('-字段名称')
#多个条件进行排序
res = models.表名.objects.order_by('字段1','字段2') #当字段1相同是会更具字段2进行排序
# select id, sum(age) as s, username from userinfo group by username
from django.db.models import Count, Min, Max, Sum
res = models.UserInfo.objects.values("name").annotate(s=Sum('age'))
# select id, sum(age) as s, username from userinfo group by username having s > 50;
res = models.UserInfo.objects.values("name").annotate(s=Sum('age')).filter(s__gt=50)
# limit 1, 3 分页
res = models.UserInfo.objects.all()[1:4]
# 因为获取对象是列表所有切片即可
# 第一条:
res = models.表名.objects.first()
# 最后一条:
res = models.表名.objects.last()
#id等于3and名字等于a
models.表名.objects.filter(id=3,and name='a').values()
# Q 对象
from django.db.models import Q
res = models.UserInfo.objects.filter(Q(id__gt=3) | Q(name='zekai')) #or用|链接
# Q 对象
from django.db.models import Q
res = models.UserInfo.objects.filter( Q(Q(id__gt=3) | Q(name='zekai')) & Q(age=23) ) and用&链接
from django.db.models import F
models.UserInfo.objects.update(name=F('name')+1) # 字段名称都加1
from django.db import connection, connections
cursor = connection.cursor() # cursor = connections['default'].cursor()
cursor.execute("""SELECT * from auth_user where id = %s""", [1])
row = cursor.fetchone()
print(row)
models.UserInfo.objects.values("name", 'age').distinct() # 前面values有多少个就对多少个值进行去除
=================================================================================
print(res.query) # 查看上述代码生成的sql语句