- Django Design Patterns and Best Practices
- Arun Ravindran
- 278字
- 2021-06-25 21:32:05
Three steps of normalization
Normalization helps you efficiently store data. Once your models are fully normalized, they will not have redundant data, and each model should contain data that is only logically related to it.
To give a quick example, if we were to normalize the post table so that we can unambiguously refer to the superhero who posted that message, then we need to isolate the user details in a separate table. Django already creates the user table by default. So, you only need to refer to the ID of the user who posted the message in the first column, as shown in the following table:

Now, it is not only clear that there were three messages posted by the same user (with an arbitrary user ID), but we can also find that user's correct name by looking up the user table.
Generally, you will design your models to be in their fully normalized form and then selectively denormalize them for performance reasons (see the next section on Performance to know why). In databases, normal forms are a set of guidelines that can be applied to a table to ensure that it is normalized. Commonly found normal forms are first, second, and third normal forms, although they could go up to the fifth normal form.
In the next example, we will normalize a table and create the corresponding Django models. Imagine a spreadsheet called Sightings that lists the first time someone spots a superhero using a power or superhuman ability. Each entry mentions the known origins, superpowers, and location of the first sighting, including latitude and longitude:

The preceding geographic data has been extracted from http://www.golombek.com/locations.html.
- MySQL數據庫應用與管理 第2版
- 青少年軟件編程基礎與實戰(圖形化編程三級)
- Scratch真好玩:教小孩學編程
- 精通網絡視頻核心開發技術
- Python機器學習基礎教程
- Mastering ROS for Robotics Programming
- .NET Standard 2.0 Cookbook
- Java EE Web應用開發基礎
- Node.js從入門到精通
- Microsoft Exchange Server 2016 PowerShell Cookbook(Fourth Edition)
- 征服C指針(第2版)
- Data Manipulation with R(Second Edition)
- Advanced C++
- Hands-On GUI Application Development in Go
- ACE技術內幕:深入解析ACE架構設計與實現原理