<table style="height: 30px; background-color: #afeeee; width: 1266px; ; width: 1266px;" border="0">
<span style="color: #008000;">#</span><span style="color: #008000;"> 大于,小于</span>
<span style="color: #008000;">#
<span style="color: #008000;">#<span style="color: #008000;"> models.Tb1.objects.filter(idgt=1) # 获取id大于1的值
<span style="color: #008000;">#<span style="color: #008000;"> models.Tb1.objects.filter(idgte=1) # 获取id大于等于1的值
<span style="color: #008000;">#<span style="color: #008000;"> models.Tb1.objects.filter(id
lt=10) # 获取id小于10的值
<span style="color: #008000;">#<span style="color: #008000;"> models.Tb1.objects.filter(idlte=10) # 获取id小于10的值
<span style="color: #008000;">#<span style="color: #008000;"> models.Tb1.objects.filter(id
lt=10,idgt=1) # 获取id大于1 且 小于10的值
<span style="color: #008000;">#</span><span style="color: #008000;"> in</span>
<span style="color: #008000;">#
<span style="color: #008000;">#<span style="color: #008000;"> models.Tb1.objects.filter(idin=[11,22,33]) # 获取id等于11、22、33的数据
<span style="color: #008000;">#<span style="color: #008000;"> models.Tb1.objects.exclude(idin=[11,33]) # not in
<span style="color: #008000;">#</span><span style="color: #008000;"> isnull</span>
<span style="color: #008000;">#</span><span style="color: #008000;"> Entry.objects.filter(pub_date__isnull=True)</span>
<span style="color: #008000;">#</span><span style="color: #008000;"> contains</span>
<span style="color: #008000;">#
<span style="color: #008000;">#<span style="color: #008000;"> models.Tb1.objects.filter(namecontains="ven")
<span style="color: #008000;">#<span style="color: #008000;"> models.Tb1.objects.filter(nameicontains="ven") # icontains大小写不敏感
<span style="color: #008000;">#<span style="color: #008000;"> models.Tb1.objects.exclude(name__icontains="ven")
<span style="color: #008000;">#</span><span style="color: #008000;"> range</span>
<span style="color: #008000;">#
<span style="color: #008000;">#<span style="color: #008000;"> models.Tb1.objects.filter(id__range=[1,2]) # 范围bettwen and
<span style="color: #008000;">#</span><span style="color: #008000;"> 其他类似</span>
<span style="color: #008000;">#
<span style="color: #008000;">#<span style="color: #008000;"> startswith,istartswith,endswith,iendswith,
<span style="color: #008000;">#</span><span style="color: #008000;"> order by</span>
<span style="color: #008000;">#
<span style="color: #008000;">#<span style="color: #008000;"> models.Tb1.objects.filter(name='seven').order_by('id') # asc
<span style="color: #008000;">#<span style="color: #008000;"> models.Tb1.objects.filter(name='seven').order_by('-id') # desc
<span style="color: #008000;">#</span><span style="color: #008000;"> group by #当values后面是annotate时候,此时不在时查询,而是分组</span>
<span style="color: #008000;">#
<span style="color: #008000;">#<span style="color: #008000;"> from django.db.models import Count,Min,Max,Sum
<span style="color: #008000;">#<span style="color: #008000;"> models.Tb1.objects.filter(c1=1).values('id').annotate(c=Count('num'))
<span style="color: #008000;">#<span style="color: #008000;"> SELECT "app01_tb1"."id",COUNT("app01_tb1"."num") AS "c" FROM "app01_tb1" WHERE "app01_tb1"."c1" = 1 GROUP BY "app01_tb1"."id"
<span style="color: #008000;">#</span><span style="color: #008000;"> limit 、offset</span>
<span style="color: #008000;">#
<span style="color: #008000;">#<span style="color: #008000;"> models.Tb1.objects.all()[10:20]
<span style="color: #008000;">#</span><span style="color: #008000;"> regex正则匹配,iregex 不区分大小写</span>
<span style="color: #008000;">#
<span style="color: #008000;">#<span style="color: #008000;"> Entry.objects.get(titleregex=r'^(An?|The) +')
<span style="color: #008000;">#<span style="color: #008000;"> Entry.objects.get(titleiregex=r'^(an?|the) +')
<span style="color: #008000;">#</span><span style="color: #008000;"> date</span>
<span style="color: #008000;">#
<span style="color: #008000;">#<span style="color: #008000;"> Entry.objects.filter(pub_date__date=datetime.date(2005,1,1))
<span style="color: #008000;">#<span style="color: #008000;"> Entry.objects.filter(pub_datedategt=datetime.date(2005,1))
<span style="color: #008000;">#</span><span style="color: #008000;"> year</span>
<span style="color: #008000;">#
<span style="color: #008000;">#<span style="color: #008000;"> Entry.objects.filter(pub_date__year=2005)
<span style="color: #008000;">#<span style="color: #008000;"> Entry.objects.filter(pub_dateyeargte=2005)
<span style="color: #008000;">#</span><span style="color: #008000;"> month</span>
<span style="color: #008000;">#
<span style="color: #008000;">#<span style="color: #008000;"> Entry.objects.filter(pub_date__month=12)
<span style="color: #008000;">#<span style="color: #008000;"> Entry.objects.filter(pub_datemonthgte=6)
<span style="color: #008000;">#</span><span style="color: #008000;"> day</span>
<span style="color: #008000;">#
<span style="color: #008000;">#<span style="color: #008000;"> Entry.objects.filter(pub_date__day=3)
<span style="color: #008000;">#<span style="color: #008000;"> Entry.objects.filter(pub_datedaygte=3)
<span style="color: #008000;">#</span><span style="color: #008000;"> week_day</span>
<span style="color: #008000;">#
<span style="color: #008000;">#<span style="color: #008000;"> Entry.objects.filter(pub_dateweek_day=2)
<span style="color: #008000;">#<span style="color: #008000;"> Entry.objects.filter(pub_dateweek_day__gte=2)
<span style="color: #008000;">#</span><span style="color: #008000;"> hour</span>
<span style="color: #008000;">#
<span style="color: #008000;">#<span style="color: #008000;"> Event.objects.filter(timestamphour=23)
<span style="color: #008000;">#<span style="color: #008000;"> Event.objects.filter(timehour=5)
<span style="color: #008000;">#<span style="color: #008000;"> Event.objects.filter(timestamphourgte=12)
<span style="color: #008000;">#</span><span style="color: #008000;"> minute</span>
<span style="color: #008000;">#
<span style="color: #008000;">#<span style="color: #008000;"> Event.objects.filter(timestampminute=29)
<span style="color: #008000;">#<span style="color: #008000;"> Event.objects.filter(timeminute=46)
<span style="color: #008000;">#<span style="color: #008000;"> Event.objects.filter(timestampminutegte=29)
<span style="color: #008000;">#</span><span style="color: #008000;"> second</span>
<span style="color: #008000;">#
<span style="color: #008000;">#<span style="color: #008000;"> Event.objects.filter(timestampsecond=31)
<span style="color: #008000;">#<span style="color: #008000;"> Event.objects.filter(timesecond=2)
<span style="color: #008000;">#<span style="color: #008000;"> Event.objects.filter(timestampsecondgte=31)
<span style="color: #0000ff;">def
<span style="color: #000000;"> all(self)
<span style="color: #008000;">#<span style="color: #008000;"> 获取所有的数据对象
<span style="color: #0000ff;">def
filter(self,*args,**<span style="color: #000000;">kwargs)
<span style="color: #008000;">#<span style="color: #008000;"> 条件查询
<span style="color: #008000;">#<span style="color: #008000;"> 条件可以是:参数,字典,Q
<span style="color: #0000ff;">def exclude(self,**<span style="color: #000000;">kwargs)
<span style="color: #008000;">#<span style="color: #008000;"> 条件查询
<span style="color: #008000;">#<span style="color: #008000;"> 条件可以是:参数,字典,Q
<span style="color: #0000ff;">def select_related(self,*<span style="color: #000000;">fields)
性能相关:跨表操作、表之间进行join连表操作,一次性获取关联的数据。
model.tb.objects.all().select_related()
model.tb.objects.all().select_related(<span style="color: #800000;">'<span style="color: #800000;">外键字段<span style="color: #800000;">') <span style="color: #008000;">#<span style="color: #008000;">多个外键使用","隔开
model.tb.objects.all().select_related(<span style="color: #800000;">'<span style="color: #800000;">外键字段__外键字段<span style="color: #800000;">'<span style="color: #000000;">)
<span style="color: #0000ff;">def prefetch_related(self,*<span style="color: #000000;">lookups)
性能相关:多表连表操作时速度会慢,使用其执行多次SQL查询在Python代码中实现连表操作,相当于把一次查询分开,先获取第一步的数据,在根据第一步数据再获取第二步数据,再把结果放在内存中
<span style="color: #008000;">#<span style="color: #008000;"> 获取所有用户表
<span style="color: #008000;">#<span style="color: #008000;"> 获取用户类型表where id in (用户表中的查到的所有用户ID)
models.UserInfo.objects.prefetch_related(<span style="color: #800000;">'<span style="color: #800000;">外键字段<span style="color: #800000;">'<span style="color: #000000;">)
</span><span style="color: #0000ff;">from</span> django.db.models <span style="color: #0000ff;">import</span><span style="color: #000000;"> Count,Case,When,IntegerField
Article.objects.annotate(
numviews</span>=<span style="color: #000000;">Count(Case(
When(readership__what_time__lt</span>=treshold,then=1<span style="color: #000000;">),output_field</span>=<span style="color: #000000;">CharField(),))
)
students </span>= Student.objects.all().annotate(num_excused_absences=<span style="color: #000000;">models.Sum(
models.Case(
models.When(absence__type</span>=<span style="color: #800000;">'</span><span style="color: #800000;">Excused</span><span style="color: #800000;">'</span>,default</span>=<span style="color: #000000;">0,output_field</span>=<span style="color: #000000;">models.IntegerField()
)))
<span style="color: #0000ff;">def annotate(self,**<span style="color: #000000;">kwargs)
<span style="color: #008000;">#<span style="color: #008000;"> 用于实现聚合group by查询
<span style="color: #0000ff;">from</span> django.db.models <span style="color: #0000ff;">import</span><span style="color: #000000;"> Count,Avg,Sum
v </span>= models.UserInfo.objects.values(<span style="color: #800000;">'</span><span style="color: #800000;">u_id</span><span style="color: #800000;">'</span>).annotate(uid=Count(<span style="color: #800000;">'</span><span style="color: #800000;">u_id</span><span style="color: #800000;">'</span><span style="color: #000000;">))
</span><span style="color: #008000;">#</span><span style="color: #008000;"> SELECT u_id,COUNT(ui) AS `uid` FROM UserInfo GROUP BY u_id</span>
<span style="color: #000000;">
v = models.UserInfo.objects.values(<span style="color: #800000;">'<span style="color: #800000;">u_id<span style="color: #800000;">').annotate(uid=Count(<span style="color: #800000;">'<span style="color: #800000;">u_id<span style="color: #800000;">')).filter(uidgt=1<span style="color: #000000;">)
<span style="color: #008000;">#<span style="color: #008000;"> SELECT u_id,COUNT(ui_id) AS uid
FROM UserInfo GROUP BY u_id having count(u_id) > 1
<span style="color: #000000;">
v = models.UserInfo.objects.values(<span style="color: #800000;">'<span style="color: #800000;">u_id<span style="color: #800000;">').annotate(uid=Count(<span style="color: #800000;">'<span style="color: #800000;">u_id<span style="color: #800000;">',distinct=True)).filter(uidgt=1<span style="color: #000000;">)
<span style="color: #008000;">#<span style="color: #008000;"> SELECT u_id,COUNT( DISTINCT ui_id) AS uid
FROM UserInfo GROUP BY u_id having count(u_id) > 1
<span style="color: #0000ff;">def distinct(self,*<span style="color: #000000;">field_names)
<span style="color: #008000;">#<span style="color: #008000;"> 用于distinct去重
models.UserInfo.objects.values(<span style="color: #800000;">'<span style="color: #800000;">nid<span style="color: #800000;">'<span style="color: #000000;">).distinct()
<span style="color: #008000;">#<span style="color: #008000;"> select distinct nid from userinfo
<span style="color: #000000;">
注:只有在PostgreSQL中才能使用distinct进行去重
<span style="color: #0000ff;">def order_by(self,*<span style="color: #000000;">field_names)
<span style="color: #008000;">#<span style="color: #008000;"> 用于排序
models.UserInfo.objects.all().order_by(<span style="color: #800000;">'<span style="color: #800000;">-id<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">age<span style="color: #800000;">'<span style="color: #000000;">)
<span style="color: #0000ff;">def extra(self,select_params=<span style="color: #000000;">None)
<span style="color: #008000;">#<span style="color: #008000;"> 构造额外的查询条件或者映射,如:子查询
<span style="color: #000000;">
Entry.objects.extra(select={<span style="color: #800000;">'<span style="color: #800000;">new_id<span style="color: #800000;">': <span style="color: #800000;">"<span style="color: #800000;">select col from sometable where othercol > %s<span style="color: #800000;">"},select_params=(1<span style="color: #000000;">,))
Entry.objects.extra(where=[<span style="color: #800000;">'<span style="color: #800000;">headline=%s<span style="color: #800000;">'],params=[<span style="color: #800000;">'<span style="color: #800000;">Lennon<span style="color: #800000;">'<span style="color: #000000;">])
Entry.objects.extra(where=[<span style="color: #800000;">"<span style="color: #800000;">foo='a' OR bar = 'a'<span style="color: #800000;">",<span style="color: #800000;">"<span style="color: #800000;">baz = 'a'<span style="color: #800000;">"<span style="color: #000000;">])
Entry.objects.extra(select={<span style="color: #800000;">'<span style="color: #800000;">new_id<span style="color: #800000;">': <span style="color: #800000;">"<span style="color: #800000;">select id from tb where id > %s<span style="color: #800000;">"},order_by=[<span style="color: #800000;">'<span style="color: #800000;">-nid<span style="color: #800000;">'<span style="color: #000000;">])
<span style="color: #0000ff;">def<span style="color: #000000;"> reverse(self):
<span style="color: #008000;">#<span style="color: #008000;"> 倒序
models.UserInfo.objects.all().order_by(<span style="color: #800000;">'<span style="color: #800000;">-nid<span style="color: #800000;">'<span style="color: #000000;">).reverse()
<span style="color: #008000;">#<span style="color: #008000;"> 注:如果存在order_by,reverse则是倒序,如果多个排序则一一倒序
<span style="color: #0000ff;">def defer(self,*<span style="color: #000000;">fields):
models.UserInfo.objects.defer(<span style="color: #800000;">'<span style="color: #800000;">username<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">id<span style="color: #800000;">'<span style="color: #000000;">)
或
models.UserInfo.objects.filter(...).defer(<span style="color: #800000;">'<span style="color: #800000;">username<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">id<span style="color: #800000;">'<span style="color: #000000;">)
<span style="color: #008000;">#<span style="color: #008000;">映射中排除某列数据
<span style="color: #0000ff;">def only(self,*<span style="color: #000000;">fields):
<span style="color: #008000;">#<span style="color: #008000;">仅取某个表中的数据
models.UserInfo.objects.only(<span style="color: #800000;">'<span style="color: #800000;">username<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">id<span style="color: #800000;">'<span style="color: #000000;">)
或
models.UserInfo.objects.filter(...).only(<span style="color: #800000;">'<span style="color: #800000;">username<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">id<span style="color: #800000;">'<span style="color: #000000;">)
<span style="color: #0000ff;">def<span style="color: #000000;"> using(self,alias):
指定使用的数据库,参数为别名(setting中的设置)
<span style="color: #008000;">#<span style="color: #008000;">#################################################<span style="color: #008000;">
<span style="color: #008000;"> PUBLIC METHODS THAT RETURN A QUERYSET SUBCLASS #<span style="color: #008000;">
<span style="color: #008000;">#################################################
<span style="color: #0000ff;">def raw(self,raw_query,translations=None,using=<span style="color: #000000;">None):
<span style="color: #008000;">#<span style="color: #008000;"> 执行原生SQL
models.UserInfo.objects.raw(<span style="color: #800000;">'<span style="color: #800000;">select * from userinfo<span style="color: #800000;">'<span style="color: #000000;">)
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 如果SQL是其他表时,必须将名字设置为当前UserInfo对象的主键列名</span>
models.UserInfo.objects.raw(<span style="color: #800000;">'</span><span style="color: #800000;">select id as nid from 其他表</span><span style="color: #800000;">'</span><span style="color: #000000;">)
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 为原生SQL设置参数</span>
models.UserInfo.objects.raw(<span style="color: #800000;">'</span><span style="color: #800000;">select id as nid from userinfo where nid>%s</span><span style="color: #800000;">'</span>,params=[12<span style="color: #000000;">,])
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 将获取的到列名转换为指定列名</span>
name_map = {<span style="color: #800000;">'</span><span style="color: #800000;">first</span><span style="color: #800000;">'</span>: <span style="color: #800000;">'</span><span style="color: #800000;">first_name</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">last</span><span style="color: #800000;">'</span>: <span style="color: #800000;">'</span><span style="color: #800000;">last_name</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">bd</span><span style="color: #800000;">'</span>: <span style="color: #800000;">'</span><span style="color: #800000;">birth_date</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">pk</span><span style="color: #800000;">'</span>: <span style="color: #800000;">'</span><span style="color: #800000;">id</span><span style="color: #800000;">'</span><span style="color: #000000;">}
Person.objects.raw(</span><span style="color: #800000;">'</span><span style="color: #800000;">SELECT * FROM some_other_table</span><span style="color: #800000;">'</span>,translations=<span style="color: #000000;">name_map)
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 指定数据库</span>
models.UserInfo.objects.raw(<span style="color: #800000;">'</span><span style="color: #800000;">select * from userinfo</span><span style="color: #800000;">'</span>,using=<span style="color: #800000;">"</span><span style="color: #800000;">default</span><span style="color: #800000;">"</span><span style="color: #000000;">)
</span><span style="color: #008000;">#</span><span style="color: #008000;">################## 原生SQL ###################</span>
<span style="color: #0000ff;">from</span> django.db <span style="color: #0000ff;">import</span><span style="color: #000000;"> connection,connections
cursor </span>= connection.cursor() <span style="color: #008000;">#</span><span style="color: #008000;"> cursor = connections['default'].cursor()</span>
cursor.execute(<span style="color: #800000;">"""</span><span style="color: #800000;">SELECT * from auth_user where id = %s</span><span style="color: #800000;">"""</span>,[1<span style="color: #000000;">])
row </span>= cursor.fetchone() <span style="color: #008000;">#</span><span style="color: #008000;"> fetchall()/fetchmany(..)</span>
<span style="color: #0000ff;">def values(self,*<span style="color: #000000;">fields):
<span style="color: #008000;">#<span style="color: #008000;"> 获取每行数据为字典格式
<span style="color: #0000ff;">def values_list(self,*fields,**<span style="color: #000000;">kwargs):
<span style="color: #008000;">#<span style="color: #008000;"> 获取每行数据为元祖
<span style="color: #0000ff;">def dates(self,field_name,kind,order=<span style="color: #800000;">'<span style="color: #800000;">ASC<span style="color: #800000;">'<span style="color: #000000;">):
<span style="color: #008000;">#<span style="color: #008000;"> 根据时间进行某一部分进行去重查找并截取指定内容
<span style="color: #008000;">#<span style="color: #008000;"> kind只能是:"year"(年),"month"(年-月),"day"(年-月-日)
<span style="color: #008000;">#<span style="color: #008000;"> order只能是:"ASC" "DESC"
<span style="color: #008000;">#<span style="color: #008000;"> 并获取转换后的时间
- year : 年-01-01
- month: 年-月-01
- day : 年-月-<span style="color: #000000;">日
models.DatePlus.objects.dates(</span><span style="color: #800000;">'</span><span style="color: #800000;">ctime</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">day</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">DESC</span><span style="color: #800000;">'</span><span style="color: #000000;">)
<span style="color: #0000ff;">def datetimes(self,order=<span style="color: #800000;">'<span style="color: #800000;">ASC<span style="color: #800000;">',tzinfo=<span style="color: #000000;">None):
<span style="color: #008000;">#<span style="color: #008000;"> 根据时间进行某一部分进行去重查找并截取指定内容,将时间转换为指定时区时间
<span style="color: #008000;">#<span style="color: #008000;"> kind只能是 "year","month","day","hour","minute","second"
<span style="color: #008000;">#<span style="color: #008000;"> order只能是:"ASC" "DESC"
<span style="color: #008000;">#<span style="color: #008000;"> tzinfo时区对象,需要安装pytz
models.DDD.objects.datetimes(<span style="color: #800000;">'<span style="color: #800000;">ctime<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">hour<span style="color: #800000;">',tzinfo=<span style="color: #000000;">pytz.UTC)
models.DDD.objects.datetimes(<span style="color: #800000;">'<span style="color: #800000;">ctime<span style="color: #800000;">',tzinfo=pytz.timezone(<span style="color: #800000;">'<span style="color: #800000;">Asia/Shanghai<span style="color: #800000;">'<span style="color: #000000;">))
</span><span style="color: #800000;">"""</span><span style="color: #800000;">
pip3 install pytz
import pytz
pytz.all_timezones
pytz.timezone(‘Asia/Shanghai’)
</span><span style="color: #800000;">"""</span>
<span style="color: #0000ff;">def<span style="color: #000000;"> none(self):
<span style="color: #008000;">#<span style="color: #008000;"> 空QuerySet对象
<span style="color: #008000;">#<span style="color: #008000;">###################################<span style="color: #008000;">
<span style="color: #008000;"> METHODS THAT DO DATABASE QUERIES #<span style="color: #008000;">
<span style="color: #008000;">###################################
<span style="color: #0000ff;">def aggregate(self,**<span style="color: #000000;">kwargs):
<span style="color: #008000;">#<span style="color: #008000;"> 聚合函数,获取字典类型聚合结果
<span style="color: #0000ff;">from django.db.models <span style="color: #0000ff;">import<span style="color: #000000;"> Count,Sum
result = models.UserInfo.objects.aggregate(k=Count(<span style="color: #800000;">'<span style="color: #800000;">u_id<span style="color: #800000;">',distinct=True),n=Count(<span style="color: #800000;">'<span style="color: #800000;">nid<span style="color: #800000;">'<span style="color: #000000;">))
===> {<span style="color: #800000;">'<span style="color: #800000;">k<span style="color: #800000;">': 3,<span style="color: #800000;">'<span style="color: #800000;">n<span style="color: #800000;">': 4<span style="color: #000000;">}
<span style="color: #0000ff;">def<span style="color: #000000;"> count(self):
<span style="color: #008000;">#<span style="color: #008000;"> 获取个数
<span style="color: #0000ff;">def get(self,**<span style="color: #000000;">kwargs):
<span style="color: #008000;">#<span style="color: #008000;"> 获取单个对象
<span style="color: #0000ff;">def create(self,**<span style="color: #000000;">kwargs):
<span style="color: #008000;">#<span style="color: #008000;"> 创建对象
<span style="color: #0000ff;">def bulk_create(self,objs,batch_size=<span style="color: #000000;">None):
<span style="color: #008000;">#<span style="color: #008000;"> 批量插入
<span style="color: #008000;">#<span style="color: #008000;"> batch_size表示一次插入的个数
objs =<span style="color: #000000;"> [
models.DDD(name=<span style="color: #800000;">'<span style="color: #800000;">r11<span style="color: #800000;">'<span style="color: #000000;">),models.DDD(name=<span style="color: #800000;">'<span style="color: #800000;">r22<span style="color: #800000;">'<span style="color: #000000;">)
]
models.DDD.objects.bulk_create(objs,10<span style="color: #000000;">)
<span style="color: #0000ff;">def get_or_create(self,defaults=None,**<span style="color: #000000;">kwargs):
<span style="color: #008000;">#<span style="color: #008000;"> 如果存在,则获取,否则,创建
<span style="color: #008000;">#<span style="color: #008000;"> defaults 指定创建时,其他字段的值
obj,created = models.UserInfo.objects.get_or_create(username=<span style="color: #800000;">'<span style="color: #800000;">root1<span style="color: #800000;">',defaults={<span style="color: #800000;">'<span style="color: #800000;">email<span style="color: #800000;">': <span style="color: #800000;">'<span style="color: #800000;">1111111<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">u_id<span style="color: #800000;">': 2,<span style="color: #800000;">'<span style="color: #800000;">t_id<span style="color: #800000;">': 2<span style="color: #000000;">})
<span style="color: #0000ff;">def update_or_create(self,**<span style="color: #000000;">kwargs):
<span style="color: #008000;">#<span style="color: #008000;"> 如果存在,则更新,否则,创建
<span style="color: #008000;">#<span style="color: #008000;"> defaults 指定创建时或更新时的其他字段
obj,created = models.UserInfo.objects.update_or_create(username=<span style="color: #800000;">'<span style="color: #800000;">root1<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">t_id<span style="color: #800000;">': 1<span style="color: #000000;">})
<span style="color: #0000ff;">def<span style="color: #000000;"> first(self):
<span style="color: #008000;">#<span style="color: #008000;"> 获取第一个
<span style="color: #0000ff;">def<span style="color: #000000;"> last(self):
<span style="color: #008000;">#<span style="color: #008000;"> 获取最后一个
<span style="color: #0000ff;">def in_bulk(self,id_list=<span style="color: #000000;">None):
<span style="color: #008000;">#<span style="color: #008000;"> 根据主键ID进行查找
id_list = [11,21,31<span style="color: #000000;">]
models.DDD.objects.in_bulk(id_list)
<span style="color: #0000ff;">def<span style="color: #000000;"> delete(self):
<span style="color: #008000;">#<span style="color: #008000;"> 删除
<span style="color: #0000ff;">def update(self,**<span style="color: #000000;">kwargs):
<span style="color: #008000;">#<span style="color: #008000;"> 更新
<span style="color: #0000ff;">def<span style="color: #000000;"> exists(self):
<span style="color: #008000;">#<span style="color: #008000;"> 是否有结果