django model操作
2015-08-17 12:03
651 查看
djangomodel类型说明:https://docs.djangoproject.com/en/1.8/ref/models/fields/1.djangomodel查询1.Person.objects.raw("select*frommyapp_person")#preson为model名返回多个model实例可以用formodelinmodels来提取一个实例
iexactBlog.objects.get(name__iexact="beatlesblog")8startswith,endswithistartswith,iendswith9外键匹配:Entry.objects.filter(blog__name='BeatlesBlog')#选择entryobjectwhere外键连接的表blog的name属性的值为'BeatlesBlog'10.数据表in,gt,lt,gte,lte,range等比较操作:
13.利用Q来进行包含或与非的查询,这是对filter的一个扩展,因为filter的参数都是按照与来进行的
forpinPerson.objects.raw('SELECT*FROMmyapp_person'): print(p) a=Person.objects.raw('SELECT*FROMmyapp_person')[0]#index使用会查询数据库
a=Person.objects.raw('SELECT*FROMmyapp_personWHERElast_name=%s',[lname])#参数使用2.filter方法,相当于简单的where语句,返回model对象Article.objects.filter(reporter__first_name='John')3.get方法,相当于简单的where语句,返回model对象Blog.objects.get(name="CheddarTalk")如果有0个或者超过一个的数据,将会报错4.all方法,相当于select*fromtable,返回表中所有tuple5.exclude方法,选择表中不满足条件的tupleq=q.exclude(body_text__icontains="food")6.delete方法,Entry.objects.all().delete()7.update方法Entry.objects.filter(blog__name=‘foo‘).update(comments_on=False)注意不能修改关联表的字段2.创建model对象,并保存到数据库
fromblog.modelsimportBlog b=Blog(name='BeatlesBlog',tagline='AllthelatestBeatlesnews.') b.save()3.改变model对象,并保存
b5.name='Newname' b5.save()4.pythonslice分片在其中的应用Entry.objects.all()[:10:2]5order_by方法:Entry.objects.order_by('headline')[0]或者Entry.objects.order_by('-headline')倒序6contains匹配相当于like也有忽略大小写的匹配icontains
#Case-sensitivecontainmenttest.Forexample: Entry.objects.get(headline__contains='Lennon')#注意是双下划线 #RoughlytranslatestothisSQL: SELECT...WHEREheadlineLIKE'%Lennon%';7.忽略大小写匹配
#下面3个操作是等价的pk表示primarykey Blog.objects.get(id__exact=14)#Explicitform Blog.objects.get(id=14)#__exactisimplied Blog.objects.get(pk=14)#pkimpliesid__exact
Blog.objects.get(pk=14) Blog.objects.filter(pk__in=[1,4,7])#pk的值在1,4,7中 Blog.objects.filter(pk__gt=14)#pk的值大于14 #range好似betweenand操作
importdatetime start_date=datetime.date(2005,1,1) end_date=datetime.date(2005,3,31) Entry.objects.filter(pub_date__range=(start_date,end_date))#SQLequivalent:
SELECT...WHEREpub_dateBETWEEN'2005-01-01'and'2005-03-31';11比较不同的列
fromdjango.db.modelsimportF Entry.objects.filter(n_comments__gt=F('n_pingbacks'))#选取n_comment大于n_pingbacks的tuple12queryset的缓存系统:当queryset被创建时cache是空的,当第一次queryset被求值时,django会将queryresult保存到cache中,然后接下来的有关该queryresult的查询操作会从cache中提取值
queryset=Entry.objects.all() print([p.headlineforpinqueryset])#Evaluatethequeryset. print([p.pub_dateforpinqueryset])#Re-usethecachefromtheevaluation. #当queryset取部分值时,django不会将queryset保存到cache中,从而第二次查询时,会再次查询数据库 queryset=Entry.objects.all() printqueryset[5]#Queriesthedatabase printqueryset[5]#Queriesthedatabaseagain
queryset=Entry.objects.all() [entryforentryinqueryset]#Queriesthedatabase printqueryset[5]#Usescache printqueryset[5]#Usescache
#以下的操作会使得django将result存储到cache中 [entryforentryinqueryset] bool(queryset) entryinqueryset list(queryset)
13.利用Q来进行包含或与非的查询,这是对filter的一个扩展,因为filter的参数都是按照与来进行的
fromdjango.db.modelsimportQ
Poll.objects.get(
Q(question__startswith='Who'),
Q(pub_date=date(2005,5,2))|Q(pub_date=date(2005,5,6))#|表示或
)
Q(question__startswith='Who')|~Q(pub_date__year=2005)#~表示取反
相关文章推荐
- 类的扩展-category和extension
- Gonet2 游戏服务器框架解析之gRPC提高(5)
- HDU 4722 Good Numbers
- GoLang反射的规则
- 中国电信欢go建立话费话费异常预警(国际)
- 【CF 507E】Breaking Good
- django 外键学习
- django 外键操作
- django对事务的处理
- 理解 Date(日期)Calendar(日历)GregorianCalendar(标准阳历)
- codeforces 560 C. Gerald's Hexagon (思维,几何)
- 【Django】上传图片之路径问题
- Django(下)
- GO语言做文件服务器小总结
- django_学习笔记0816
- POJ2007--Scrambled Polygon
- hdu 4983 Goffi and GCD(欧拉函数)
- Beanstalkd的使用(Golang)
- NYOJ 635 Oh, my goddess (简单BFS)
- Django(中)