We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The road to success is lined with many tempting parking spaces.
Retrieving objects
Django
ORM
get()
Post.objects.get()
manager
objects
all()
Post.objects.all()
QuerySet 只有被命令执行的时候,才会去执行
QuerySet
>>>all_post = Post.objects.all()
all_post
>>>Post.objects.all()
Using the filter() method
filter()
Post.objects.filter(publish__year=2015)
Post.objects.filter(publish__year=2015, author__username='admin')
Using exclude()
exclude()
Post.objects.filter(publish__year=2015).exclude(title__startswith='Why')
Using order_by()
order_by()
Post.objects.order_by('title')
-
Post.objects.order_by('-title')
Deleting objects
delete()
post = Post.objects.get(id=1) post.delete()
Object Relational Mapping
SQL
The text was updated successfully, but these errors were encountered:
No branches or pull requests
0x01 Django
Retrieving objects
Django
有一套自己的ORM
模型,检索一个对象的时候,可以用get()
方法(Post.objects.get()
),每一个Django
模型都有至少一个manager
,默认的manager
就是objects
,检索一个表中所有的对象时,使用all()
方法(Post.objects.all()
)QuerySet
只有被命令执行的时候,才会去执行>>>all_post = Post.objects.all()
不会执行,因为all_post
没有任何操作(没回显)>>>Post.objects.all()
会执行,因为强迫其输出了结果(有回显)Using the filter() method
QuerySet
,搜索指定条件,可以利用filter()
方法Post.objects.filter(publish__year=2015)
Post.objects.filter(publish__year=2015, author__username='admin')
Using exclude()
exclude()
方法,比如:Post.objects.filter(publish__year=2015).exclude(title__startswith='Why')
Using order_by()
order_by()
方法,比如:Post.objects.order_by('title')
-
:Post.objects.order_by('-title')
Deleting objects
delete()
方法0x02 ORM
ORM
模型Object Relational Mapping
对象关系映射ORM
是通过使用描述对象和数据库之间映射的元数据,将程序中的对象持久化到关系数据库中。ORM
模型大概的思想就是对象和数据库中表的对应关系,一个对象就好比表中的一行数据,将对象的操作视为对表的操作,如增、删、改、查就是对表的同样操作,只需要封装一下就可以省下很多的重复劳动,通过ORM
模型可以对代码上进行简洁,对数据库的操作通过ORM
转换后执行,可以省掉很多复杂的SQL
组装。The text was updated successfully, but these errors were encountered: