- Django Design Patterns and Best Practices
- Arun Ravindran
- 578字
- 2021-07-23 19:36:22
M is bigger than V and C
In Django, models are classes that provide an object-oriented way of dealing with databases. Typically, each class refers to a database table and each attribute refers to a database column. You can make queries to these tables using an automatically generated API.
Models can be the base for many other components. Once you have a model, you can rapidly derive model admins, model forms, and all kinds of generic views. In each case, you would need to write a line of code or two, just so that it does not seem too magical.
Also, models are used in more places than you would expect. This is because Django can be run in several ways. Some of the entry points of Django are as follows:
- The familiar web request-response flow
- Django interactive shell
- Management commands
- Test scripts
- Asynchronous task queues such as Celery
In almost all these cases, the model modules would get imported (as a part of django.setup()
). Hence, it is best to keep your models free from any unnecessary dependencies or to import any other Django components such as views.
In short, designing your models properly is quite important. Now let's get started with the SuperBook model design.
Note
The Brown Bag Lunch
Author's Note: The progress of the SuperBook project will appear in a box like this. You may skip the box but you will miss the insights, experiences, and drama of working in a web application project.
Steve's first week with his client, the SuperHero Intelligence and Monitoring or S.H.I.M. for short, was a mixed bag. The office was incredibly futuristic but getting anything done needed a hundred approvals and sign-offs.
Being the lead Django developer, Steve had finished setting up a mid-sized development server hosting four virtual machines over two days. The next morning, the machine itself had disappeared. A washing machine-sized robot nearby said that it had been taken to the forensic department due to unapproved software installations.
The CTO, Hart was, however, of great help. He asked the machine to be returned in an hour with all the installations intact. He had also sent pre-approvals for the SuperBook project to avoid any such roadblocks in future.
Later that afternoon, Steve was having a brown-bag lunch with him. Dressed in a beige blazer and light blue jeans, Hart arrived well in time. Despite being taller than most people and having a clean-shaven head, he seemed cool and approachable. He asked if Steve had checked out the previous attempt to build a superhero database in the sixties.
"Oh yes, the Sentinel project, right?" said Steve. "I did. The database seemed to be designed as an Entity-Attribute-Value model, something that I consider an anti-pattern. Perhaps they had very little idea about the attributes of a superhero those days." Hart almost winced at the last statement. In a slightly lowered voice, he said, "You are right, I didn't. Besides, they gave me only two days to design the whole thing. I believe there was literally a nuclear bomb ticking somewhere."
Steve's mouth was wide open and his sandwich had frozen at its entrance. Hart smiled. "Certainly not my best work. Once it crossed about a billion entries, it took us days to run any kind of analysis on that damn database. SuperBook would zip through that in mere seconds, right?"
Steve nodded weakly. He had never imagined that there would be around a billion superheroes in the first place.
- Clojure Programming Cookbook
- Learning Single:page Web Application Development
- Testing with JUnit
- PostgreSQL技術內幕:事務處理深度探索
- Elastic Stack應用寶典
- The Complete Coding Interview Guide in Java
- Python忍者秘籍
- C#應用程序設計教程
- 軟件品質之完美管理:實戰經典
- 機器學習微積分一本通(Python版)
- Akka入門與實踐
- C語言程序設計教程
- Cinder:Begin Creative Coding
- Unity Certified Programmer:Exam Guide
- Python for Secret Agents