3.4.5 Adding Methods to Django Models

Because Django models are Python classes, developers can add behavior to the model by writing class methods. Doing so is as straightforward as writing any Python method because fields act like class attributes within Python.

For the moment, we will not expand on the behavior of any of our models. The entire purpose of Part I is to learn how Django works at its core, and expanding Django functionality this early in the game would detract from this goal.

However, it is possible to write methods that supplement model behavior. Django actually expects and encourages developers to write two such methods for each model. We wait until Chapter 6 to see the second method but we write the first, the string method, ...

Get Django Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.