- Django Design Patterns and Best Practices
- Arun Ravindran
- 143字
- 2021-06-25 21:32:10
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.
- 手機(jī)安全和可信應(yīng)用開發(fā)指南:TrustZone與OP-TEE技術(shù)詳解
- Android Jetpack開發(fā):原理解析與應(yīng)用實(shí)戰(zhàn)
- Learn to Create WordPress Themes by Building 5 Projects
- Rust實(shí)戰(zhàn)
- C語言程序設(shè)計(jì)
- Mastering macOS Programming
- Hands-On Enterprise Automation with Python.
- C# 8.0核心技術(shù)指南(原書第8版)
- OpenCV 3 Blueprints
- Xamarin Blueprints
- Practical GIS
- Python大規(guī)模機(jī)器學(xué)習(xí)
- 人人都能開發(fā)RPA機(jī)器人:UiPath從入門到實(shí)戰(zhàn)
- Roslyn Cookbook
- PHP從入門到精通(第7版)