- Django 2 by Example
- Antonio Melé
- 178字
- 2021-06-25 21:19:01
Retrieving objects
The Django object-relational mapping (ORM) is based on QuerySets. A QuerySet is a collection of objects from your database that can have several filters to limit the results. You already know how to retrieve a single object from the database using the get() method. We have accessed this method using Post.objects.get(). Each Django model has at least one manager, and the default manager is called objects. You get a QuerySet object using your model manager. To retrieve all objects from a table, you just use the all() method on the default objects manager, like this:
>>> all_posts = Post.objects.all()
This is how we create a QuerySet that returns all objects in the database. Note that this QuerySet has not been executed yet. Django QuerySets are lazy; they are only evaluated when they are forced to. This behavior makes QuerySets very efficient. If we don't set the QuerySet to a variable, but instead write it directly on the Python shell, the SQL statement of the QuerySet is executed because we force it to output results:
>>> Post.objects.all()
- 物聯網+BIM:構建數字孿生的未來
- 農產品物聯網研究與應用
- 物聯網關鍵技術及應用
- 面向物聯網的嵌入式系統開發:基于CC2530和STM32微處理器
- 網絡安全技術與解決方案(修訂版)
- 企業私有云建設指南
- Getting Started with WebRTC
- Getting Started with Grunt:The JavaScript Task Runner
- 通信原理及MATLAB/Simulink仿真
- Learning Node.js Development
- Hands-On Microservices with Node.js
- 深入理解Nginx:模塊開發與架構解析
- 商業的本質和互聯網
- Intelligent Mobile Projects with TensorFlow
- Python Web Scraping Cookbook