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

Problem details

Every Django model has a default manager called objects. Invoking objects.all(), will return all the entries for that model in the database. Usually, we are interested in only a subset of all entries.

We apply various filters to find out the set of entries we need. The criterion to select them is often our core business logic. For example, we can find the posts accessible to the public by the following code:

public = Posts.objects.filter(privacy="public") 

This criterion might change in the future. For example, we might want to also check whether the post was marked for editing. This change might look as follows:

public = Posts.objects.filter(privacy=POST_PRIVACY.Public, 
         draft=False) 

However, this change needs to be made everywhere a public post is needed. This can get very frustrating. There needs to be only one place to define such commonly used queries without repeating oneself.

主站蜘蛛池模板: 南昌市| 达拉特旗| 松桃| 万载县| 定结县| 汶川县| 温泉县| 鄂伦春自治旗| 宜都市| 东乡县| 布尔津县| 永年县| 北辰区| 普宁市| 郧西县| 缙云县| 吴桥县| 崇信县| 泸定县| 三明市| 娄底市| 肇源县| 黔西| 禹城市| 桃园县| 潢川县| 静海县| 上蔡县| 大新县| 肥西县| 晋宁县| 宁安市| 双鸭山市| 榕江县| 东丽区| 咸宁市| 玉门市| 河北区| 龙海市| 伊川县| 郓城县|