5.单表操作

一、基本增删改查操作

   

models.Tb1.objects.create(c1='xx', c2='oo')

# 增加一条数据,可以接受字典类型数据 ** kwargs

obj = models.Tb1(c1='xx', c2='oo')

obj.save()

#

models.Tb1.objects.get(id=123) # 获取单条数据,不存在则报错(不建议)

models.Tb1.objects.all() # 获取全部

models.Tb1.objects.filter(name='seven') # 获取指定条件的数据,如果没有就返回一个空的QerrySet

返回的数据是QerrySet类型。我们可以理解成列表,所以可以用切片处理。objects是数据库管理器。

   

degree = models.CharField(choices=(("cj", u"初级"), ("zj", u"中级"), ('gj', u"高级")), verbose_name=u"课程难度", max_length=2)

这种情况下,在django中如果直接用course.degree 的话会显示cj,zj,gj,如果我们想要显示初级,高级等,可以用course.get_degree_display 方法。这是一种固定写法,中间的degree是字段名字。是专门针对choice字段的显示。

   

#

objs=models.Tb1.objects.filter(name='seven')

objs.delete() # 删除指定条件的数据,也即是删除整个QerrySet的数据

for obj in objs:

obj.delete # 删除单条数据

#

models.Tb1.objects.filter(name='seven').update(gender='0') # 将指定条件的数据更新,均支持 **kwargs

   

obj = models.Tb1.objects.get(id=1)

obj.c1 = '111'

obj.save()# 修改单条数据

   

二、进阶操作

了不起的双下划线,利用双下划线将字段和对应的操作连接起来

models.Tb1.objects.filter(name='seven').count() # 获取个数

   

# 大于,小于

   

models.Tb1.objects.filter(id__gt=1) # 获取id大于1的值

models.Tb1.objects.filter(id__gte=1) # 获取id大于等于1的值

models.Tb1.objects.filter(id__lt=10) # 获取id小于10的值

models.Tb1.objects.filter(id__lte=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等于112233的数据

models.Tb1.objects.exclude(id__in=[11, 22, 33]) # not in

   

# isnull

Entry.objects.filter(pub_date__isnull=True)

   

# 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. 其他类似 startswithistartswith, endswith, iendswith,

   

# order by

   

models.Tb1.objects.filter(name='seven').order_by('id') # asc

models.Tb1.objects.filter(name='seven').order_by('-id') # desc

   

# group by

   

from django.db.models import Count

   

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"

   

# limit offset

   

models.Tb1.objects.all()[10:20]

   

# regex正则匹配,iregex 不区分大小写

   

Entry.objects.get(title__regex=r'^(An?|The) +')

Entry.objects.get(title__iregex=r'^(an?|the) +')

   

# date

   

Entry.objects.filter(pub_date__date=datetime.date(2005, 1, 1))

Entry.objects.filter(pub_date__date__gt=datetime.date(2005, 1, 1))

   

# year

   

Entry.objects.filter(pub_date__year=2005)

Entry.objects.filter(pub_date__year__gte=2005)

   

# month

   

Entry.objects.filter(pub_date__month=12)

Entry.objects.filter(pub_date__month__gte=6)

   

# day

   

Entry.objects.filter(pub_date__day=3)

Entry.objects.filter(pub_date__day__gte=3)

   

# week_day

   

Entry.objects.filter(pub_date__week_day=2)

Entry.objects.filter(pub_date__week_day__gte=2)

   

# hour

   

Event.objects.filter(timestamp__hour=23)

Event.objects.filter(time__hour=5)

Event.objects.filter(timestamp__hour__gte=12)

   

# minute

   

Event.objects.filter(timestamp__minute=29)

Event.objects.filter(time__minute=46)

Event.objects.filter(timestamp__minute__gte=29)

   

# second

   

Event.objects.filter(timestamp__second=31)

Event.objects.filter(time__second=2)

Event.objects.filter(timestamp__second__gte

   

三、其他操作

1F 操作

# An object capable of resolving references to existing query objects

   

from django.db.models import F

models.Tb1.objects.update(num=F('num') + 1)

   

2Q方法(或操作)

   

Q(nid__gt=10)

Q(nid=8) | Q(nid__gt=10)

Q(Q(nid=8) | Q(nid__gt=10)) & Q(caption='root')

   

3、执行原生SQL

   

from django.db import connection

   

cursor = connection.cursor() # cursor = connections['default'].cursor()

cursor.execute("""SELECT * from auth_user where id = %s""", [1])

row = cursor.fetchone()

   

   

猜你喜欢

转载自www.cnblogs.com/liuqianli/p/8975559.html