Book Image

Mastering Django: Core

By : Nigel George
Book Image

Mastering Django: Core

By: Nigel George

Overview of this book

Mastering Django: Core is a completely revised and updated version of the original Django Book, written by Adrian Holovaty and Jacob Kaplan-Moss - the creators of Django. The main goal of this book is to make you a Django expert. By reading this book, you’ll learn the skills needed to develop powerful websites quickly, with code that is clean and easy to maintain. This book is also a programmer’s manual that provides complete coverage of the current Long Term Support (LTS) version of Django. For developers creating applications for commercial and business critical deployments, Mastering Django: Core provides a complete, up-to-date resource for Django 1.8LTS with a stable code-base, security fixes and support out to 2018.
Table of Contents (33 chapters)
Mastering Django: Core
Credits
About the Author
www.PacktPub.com
Preface
Free Chapter
1
Introduction to Django and Getting Started

Universal field options


Table A.2 lists all the optional field arguments in Django. They are available to all field types.

Option

Description

null

If True, Django will store empty values as NULL in the database. Default is False. Avoid using null on string-based fields such as CharField and TextField because empty string values will always be stored as empty strings, not as NULL. For both string-based and non-string-based fields, you will also need to set blank=True if you wish to permit empty values in forms. If you want to accept null values with BooleanField, use NullBooleanField instead.

blank

If True, the field is allowed to be blank. Default is False. Note that this is different than null. null is purely database-related, whereas blank is validation-related.

choices

An iterable (for example, a list or tuple) consisting itself of iterables of exactly two items (for example, [(A, B), (A, B) ...]) to use as choices for this field. If this is given, the default form widget will be a select box with these choices instead of the standard text field. The first element in each tuple is the actual value to be set on the model, and the second element is the human-readable name.

db_column

The name of the database column to use for this field. If this isn't given, Django will use the field's name.

db_index

If True, a database index will be created for this field.

db_tablespace

The name of the database tablespace to use for this field's index, if this field is indexed. The default is the project's DEFAULT_INDEX_TABLESPACE setting, if set, or the db_tablespace of the model, if any. If the backend doesn't support tablespaces for indexes, this option is ignored.

default

The default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created. The default cannot be a mutable object (model instance, list, set, and others.), as a reference to the same instance of that object would be used as the default value in all new model instances.

editable

If False, the field will not be displayed in the admin or any other ModelForm. They are also skipped during model validation. Default is True.

error_messages

The error_messages argument lets you override the default messages that the field will raise. Pass in a dictionary with keys matching the error messages you want to override. Error message keys include null, blank, invalid, invalid_choice, unique, and unique_for_date.

help_text

Extra help text to be displayed with the form widget. It's useful for documentation even if your field isn't used on a form. Note that this value is not HTML-escaped in automatically-generated forms. This lets you include HTML in help_text if you so desire.

primary_key

If True, this field is the primary key for the model. If you don't specify primary_key=True for any field in your model, Django will automatically add an AutoField to hold the primary key, so you don't need to set primary_key=True on any of your fields unless you want to override the default primary-key behavior. The primary key field is read-only.

unique

If True, this field must be unique throughout the table. This is enforced at the database level and by model validation. This option is valid on all field types except ManyToManyField, OneToOneField, and FileField.

unique_for_date

Set this to the name of a DateField or DateTimeField to require that this field be unique for the value of the date field. For example, if you have a field title that has unique_for_date="pub_date", then Django wouldn't allow the entry of two records with the same title and pub_date. This is enforced by Model.validate_unique() during model validation but not at the database level.

unique_for_month

Like unique_for_date, but requires the field to be unique with respect to the month.

unique_for_year

Like unique_for_date, but requires the field to be unique with respect to the year.

verbose_name

A human-readable name for the field. If the verbose name isn't given, Django will automatically create it using the field's attribute name, converting underscores to spaces.

validators

A list of validators to run for this field.

Table A.2: Django universal field options