Book Image

Django Essentials

By : Samuel Dauzon
Book Image

Django Essentials

By: Samuel Dauzon

Overview of this book

<p>Django is a powerful Python web framework designed for rapid web application development. With the advent of frameworks such as Django, web developers have been forced to adopt MVC architectures and are encouraged to develop quality code. This quality allows several developers to work together easily and reduces the number of bugs due to human errors.</p> <p>Beginning with the basics of the Web and Django, the book explains the MVC pattern. It then moves on to explain the step-by-step installation of Python, PIP, and Django on Windows, Linux, and Mac OS. Furthermore, you will learn how to create templates, models, forms, and so on. After reading the book, you will be able to quickly create dynamic web applications with AJAX and an admin part.</p> <p>This book features a step-by-step approach that shows you how to program, create, and improve the quality of web applications using Django, with the help of Python.</p>
Table of Contents (20 chapters)
Django Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Advanced usage of models


We studied the basics of the models that allow us to create simple applications. Sometimes, it is necessary to define more complex structures.

Using two relationships for the same model

Sometimes, it is useful to store two foreign keys (or more) in a single model. For example, if we want two developers to work in parallel on the same task, we must use the related_name property in our models. For example, our Task model contains two relationships with the following lines:

developer1 = models.ForeignKey (Developer , verbose_name = "User" , related_name = "dev1" )
developer2 = models.ForeignKey (Developer , verbose_name = "User" , related_name = "dev2" )

Further in this book, we will not use these two relationships. To effectively follow this book, we must return to our previously defined Task model.

Note

Here, we define two developers on the same task. Best practices advise us to create a many-to-many relationship in the Task model. The thorough argument allows you to specify...