官术网_书友最值得收藏!

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()
主站蜘蛛池模板: 新野县| 浙江省| 开阳县| 娱乐| 霍林郭勒市| 唐河县| 东乡县| 武义县| 阿拉善盟| 鄂托克前旗| 宕昌县| 浦城县| 新绛县| 县级市| 上高县| 米林县| 东海县| 晴隆县| 额尔古纳市| 柞水县| 宁波市| 贞丰县| 乌兰察布市| 辉县市| 镇坪县| 苏州市| 杭锦旗| 商城县| 淮安市| 廉江市| 宣武区| 获嘉县| 洪雅县| 兰州市| 嘉义市| 乌鲁木齐市| 静安区| 阿拉善左旗| 枣庄市| 夏邑县| 三门峡市|