- Django Design Patterns and Best Practices
- Arun Ravindran
- 196字
- 2021-06-25 21:32:07
Problem details
While designing models, you might find certain common attributes or behaviors shared across model classes. For example, a post and comment model needs to keep track of its created date and modified date. Manually copying and pasting the fields and their associated method is not a very DRY approach.
Since Django models are classes, object-oriented approaches such as composition and inheritance are possible solutions. However, compositions (by having a property that contains an instance of the shared class) will need an additional level of indirection to access fields.
Inheritance can get tricky. We can use a common base class for post and comments. However, there are three kinds of inheritance in Django: concrete, abstract, and proxy.
Concrete inheritance works by deriving from the base class just like you normally would in Python classes. However, in Django, this base class will be mapped into a separate table. Each time you access base fields, an implicit join is needed. This leads to horrible performance.
Proxy inheritance can only add new behavior to the parent class. You cannot add new fields. Hence, it is not very useful for this situation.
Finally, we are left with Abstract inheritance.
- Node.js Design Patterns
- aelf區塊鏈應用架構指南
- 小程序,巧運營:微信小程序運營招式大全
- Nginx Essentials
- Mastering AndEngine Game Development
- PhoneGap Mobile Application Development Cookbook
- 嚴密系統設計:方法、趨勢與挑戰
- Python時間序列預測
- INSTANT Passbook App Development for iOS How-to
- 基于ARM Cortex-M4F內核的MSP432 MCU開發實踐
- Emotional Intelligence for IT Professionals
- Python編程基礎教程
- 零基礎學Java(第5版)
- Learning iOS Penetration Testing
- 詩意的邊緣