Python自动化之django的ORM
生活随笔
收集整理的這篇文章主要介紹了
Python自动化之django的ORM
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
django ORM操作
1.什么是ORM?
ORM,即Object-Relational Mapping(對象關系映射),它的作用是在關系型數據庫和業務實體對象之間作一個映射,這樣,我們在具體的操作業務對象的時候,就不需要再去和復雜的SQL語句打交道,只需簡單的操作對象的屬性和方法。
select * from tb where id > 1 # 對應關系 models.tb.objects.filter(id__gt=1) models.tb.objects.filter(id=1) models.tb.objects.filter(id__lt=1)創建類a. 先寫類from django.db import models# app01_userinfoclass UserInfo(models.Model):# id列,自增,主鍵# 用戶名列,字符串類型,指定長度username = models.CharField(max_length=32)password = models.CharField(max_length=64)配置連接MySQL
DATABASES = {'default': {'ENGINE': 'django.db.backends.mysql','NAME':'dbname','USER': 'root','PASSWORD': 'xxx','HOST': '','PORT': '',} }# 由于Django內部連接MySQL時使用的是MySQLdb模塊,而python3中還無此模塊,所以需要使用pymysql來代替# 如下設置放置的與project同名的配置的 __init__.py文件中import pymysql pymysql.install_as_MySQLdb()注冊APP
a. 注冊APPINSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','app01',] b. 執行命令python manage.py makemigrations //會生成記錄數據庫的改動的文件python manage.py migrate //把改動應用到數據庫增加數據
models.Tb1.objects.create(c1='xx', c2='oo') 增加一條數據,可以接受字典類型數據 **kwargsobj = models.Tb1(c1='xx', c2='oo')# obj.save()Tb1是類名
刪除數據
models.Tb1.objects.filter(name='seven').delete()修改
#更新一條數據 color_obj=models.Colors.objects.get(colors="黑") color_obj.colors="灰" color_obj.save() #更新多條數據,把滿足條件的球的description都變為灰球 #寫法1: models.Ball.objects.filter(color__colors="紅").update(description="灰球") #寫法2: up_dic={"description":"灰球"} models.Ball.objects.filter(id__gt=0).update(**up_dic)查
# models.Tb1.objects.get(id=123) # 獲取單條數據,不存在則報錯(不建議) # models.Tb1.objects.all() # 獲取全部 # models.Tb1.objects.filter(name='seven') # 獲取指定條件的數據其他操作
# 獲取個數 # # models.Tb1.objects.filter(name='seven').count()# 大于,小于 # # models.Tb1.objects.filter(id__gt=1) # 獲取id大于1的值 # models.Tb1.objects.filter(id__lt=10) # 獲取id小于10的值 # models.Tb1.objects.filter(id__lt=10, id__gt=1) # 獲取id大于1 且 小于10的值# in # # models.Tb1.objects.filter(id__in=[11, 22, 33]) # 獲取id等于11、22、33的數據 # models.Tb1.objects.exclude(id__in=[11, 22, 33]) # not in# contains # # models.Tb1.objects.filter(name__contains="ven") # models.Tb1.objects.filter(name__icontains="ven") # icontains大小寫不敏感 # models.Tb1.objects.exclude(name__icontains="ven")# range # # models.Tb1.objects.filter(id__range=[1, 2]) # 范圍bettwen and# 其他類似 # # startswith,istartswith, endswith, iendswith,# order by # # models.Tb1.objects.filter(name='seven').order_by('id') # asc # models.Tb1.objects.filter(name='seven').order_by('-id') # desc# limit 、offset # # models.Tb1.objects.all()[10:20]# group by from django.db.models import Count, Min, Max, Sum # models.Tb1.objects.filter(c1=1).values('id').annotate(c=Count('num')) # SELECT "app01_tb1"."id", COUNT("app01_tb1"."num") AS "c" FROM "app01_tb1" WHERE "app01_tb1"."c1" = 1 GROUP BY "app01_tb1"."id"進階操作
轉載于:https://www.cnblogs.com/wspblog/p/6170742.html
總結
以上是生活随笔為你收集整理的Python自动化之django的ORM的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 20145203 《信息安全系统设计基础
- 下一篇: JQuery.validate.js 表