Two Scoops Django 推荐的数据模型最佳实践

添加或修改数据模型都不能马虎,有关数据的操作都需慎重考虑。

推荐使用的 Django 数据模型相关的包:

  1. django-model-utils: 使用其 TimeStampedModel
  2. django-extensions: 使用其管理命令 shell_plus,它会自动加载所有已安装应用的数据模型

基础

将具有很多数据模型的应用进行拆分

推荐每个应用的数据模型数不超过 5 个。如果一个应用的数据模型数太多,意味着该应用做的事太多了,需要进行拆分。

慎重选择数据模型继承方式

Django 支持三种继承方式:

  1. 抽象基类
  2. 多表继承
  3. 代理模型

Django 抽象基类和 Python 的抽象基类是不同的!,它们有不同的目的和行为。

各种继承方式的优缺点:

继承方式 | 优点 | 缺点
------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------|
抽象基类:只有继承的子数据模型才会创建数据表 | 能在抽象父类中定义共同项来减少重复输入,同时没有多表继承的额外数据表和 join 操作的开销 | 父类不能单独使用
多表继承:父类和子类都会创建对应的数据表。两者之间隐含有一个 OneToOneField 关联 | 因每个数据模型都有表,故可对父子各自进行查询操作。同时可以通过 parent.child 从父对象直接访问子对象 | 对子表的查询都会有一个与其所有父表的 join 操作。非常不推荐使用多表继承!
代理模型:只为原始数据模型创建数据表 | 可以为原始数据模型创建一个别名,并添加不同的 Python 行为 | 无法修改数据模型项

如何确定应该使用哪种继承方式:

  • 如果重叠量很少(只有一两个项),则不需要用继承,只需在两个数据模型中都进行定义
  • 如果两者间有较多的重复项,则应重构代码,将相同项放置到一个抽象基类中
  • 代理模型有时会很有用,但它与其它两种模型继承方式非常不同
  • 应避免使用多表继承,因其即增加了复杂度又提高了性能开销。可以用 OneToOneFieldForeignKeys 来代替。

数据模型继承实践: TimeStampedModel

在数据模型中增加 createdmodified 两个时间戳项是个普遍的需求。可以写一个 TimeStampedModel 基类如下:

# core/models.py
from django.db import models

class TimeStampedModel(models.Model):
    """
    An a
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Two Scoops of Django 1.11 Will Help You Build Django Projects. In this book we introduce you to the various tips, tricks, patterns, code snippets, and techniques that we've picked up over the years. We have put thousands of hours into the fourth edition of the book, writing and revising its material to include significant improvements and new material based on feedback from previous editions. Table of Contents Chapter 1: Coding Style Chapter 2: The Optimal Django Environment Setup Chapter 3: How To Lay Out Django Projects Chapter 4: Fundamentals of Django App Design Chapter 5: Settings and Requirements Files Chapter 6: Model Best Practices Chapter 7: Queries and the Database Layer Chapter 8: Function- and Class-Based Views Chapter 9: Best Practices for Function-Based Views Chapter 10: Best Practices for Class-Based Views Chapter 11: Form Fundamentals Chapter 12: Common Patterns for Forms Chapter 13: Templates: Best Practices Chapter 14: Template Tags and Filters Chapter 15: Django Templates and Jinja2 Chapter 16: Building APIs with Django Rest Framework Chapter 17: Consuming REST APIs Chapter 18: Tradeoffs of Replacing Core Components Chapter 19: Working With the Django Admin Chapter 20: Dealing with the User Model Chapter 21: Django's Secret Sauce: Third-Party Packages Chapter 22: Testing Chapter of Doom! Chapter 23: Documentation: Be Obsessed Chapter 24: Finding and Reducing Bottlenecks Chapter 25: Asynchronous Task Queues Chapter 26: Security Best Practices Chapter 27: Logging: Tips and Tools Chapter 28: Signals: Use Cases and Avoidance Techniques Chapter 29: What About Those Random Utilities? Chapter 30: Deployment: Platforms as a Service Chapter 31: Deploying Django Projects Chapter 29: Identical Environments: The Holy Grail Chapter 32: Continuous Integration Chapter 33: The Art of Debugging Chapter 34: Where and How to Ask Django Questions Chapter 35: Closing Thoughts Appendix A: Packages Mentioned In This Book Appendix B: Troubleshooting Appendix C: Additional Resources Appendix D: Internationalization and Localization Appendix E: Settings Alternatives Appendix F: Working with Python 2 Appendix G: Channels and Websockets What People Say About Two Scoops of Django This is the swiss army knife for every Django developer. -- Jannis Gebauer, djangopackages.org maintainer and pyup.io founder We buy this book for every new engineer on our team. It's a must for Django development! -- Jacinda Shelly, CTO of Doctor On Demand I wanted to write a book about best practices in Django, except Two Scoops is that book, no need to write another one. -- Buddy Lindsey, Host of GoDjango Audrey's illustrations reinforce Audrey and Daniel's Django technical excellence. (Art + ice cream) * 2 tech experts = Two Scoops of Django. -- Carol Willing, Project Jupyter Core Dev and Python Software Foundation director Simply the best book on Django. Whenever I am not sure if I am following the best practices, I look up the topic in this book. A must read. -- Abu Ashraf Masnun, programmer

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值