Book Image

Django By Example

By : Antonio Melé
Book Image

Django By Example

By: Antonio Melé

Overview of this book

<p>Django is a powerful Python web framework designed to develop web applications quickly, from simple prototypes to large-scale projects. Django encourages clean, pragmatic design, and provides developers with a comprehensive set of tools to build scalable web applications.</p> <p>This book will walk you through the creation of four professional Django projects, teaching you how to solve common problems and implement best practices.</p> <p>The book begins by showing you how to build a blog application, before moving on to developing a social image bookmarking website, an online shop and an e-learning platform. You will learn how to build a search engine and implement a user activity stream. Furthermore, you will create a recommendation engine, an e-commerce coupon system and a content management system. The book will also teach you how to enhance your applications with AJAX, create RESTful APIs and setup a production environment for your Django projects.</p> <p>After reading this book, you will have a good understanding of how Django works and how to integrate it with other technologies to build practical, advanced, web applications.</p>
Table of Contents (19 chapters)
Django By Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating custom model fields


Django comes with a complete collection of model fields that you can use to build your models. However, you can also create your own model fields to store custom data or alter the behavior of existing fields.

We need a field that allows us to specify an order for objects. If you think about an easy way to do this with a field provided by Django, you will probably think of adding a PositiveIntegerField to your models. This is a good starting point. We can create a custom field that inherits from PositiveIntegerField and provides additional behavior.

There are two relevant functionalities that we will build into our order field:

  • Automatically assign an order value when no specific order is provided. When no order is provided while storing an object, our field should automatically assign the next order based on the last existing ordered object. If there are two objects with order 1 and 2 respectively, when saving a third object, we should automatically assign the order...