Appendix A. Model Definition Reference

Chapter 4, Models, explains the basics of defining models, and we use them throughout the rest of the book. There is, however, a huge range of model options available not covered elsewhere. This appendix explains each possible model definition option.

Fields

The most important part of a model-and the only required part of a model-is the list of database fields it defines.

Field name restrictions

Django places only two restrictions on model field names:

  1. A field name cannot be a Python reserved word, because that would result in a Python syntax error. For example:
            class Example(models.Model): 
            pass = models.IntegerField() # 'pass' is a reserved word! 
    
  2. A field name cannot contain more than one underscore in a row, ...

Get Mastering Django: Core 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.