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

The field types in models


The following sections cover a nonexhaustive list of the field types in models.

The model fields are those that will be saved in the database. Depending on the database system selected, the type field may be different depending on the database used.

The types are specified with their options in the following manner:

Type (option1 = example_data, option2 = example_data) [information]

The numerical field type

Fields presented in this section are numeric fields such as integers and decimals:

  • SmallIntegerField(): This defines a small integer field; for some databases, the lower value is 256

  • IntegerField(): This defines an integer field

  • BigIntegerField(): Accuracy is 64 bits, from -9223372036854775808 to 9223372036854775807

  • DecimalField (max_digits = 8, decimal_places = 2)

The descriptions of the options are as follows:

  • max_digits: This sets the number of digits that make up the whole number

  • decimal_places: This sets the number of digits that compose the decimal part of the number

The string field type

This section contains the types of fields that contain strings:

  • CharField (max_length = 250)

  • TextField (max_length = 250): This field has the distinction of being presented as a <textarea> tag in the Django forms

  • EmailField (max_length = 250): This field is CharField that contains an e-mail validator for Django forms

The description of the option is as follows:

  • max_length: This sets the maximum number of characters that compose the string

The temporal field type

This section contains the types of fields that contain temporal data:

  • DateField (auto_now = false, auto_now_add = true)

  • DateTimeField (auto_now = false, auto_now_add = true)

  • TimeField (auto_now = false, auto_now_add = true)

The descriptions of the options are as follows:

  • auto_now: This automatically sets the field to the current time each time a record is saved

  • auto_now_add: This automatically sets the field to the current time when an object is created

Other types of fields

This section contains the types of fields that do not belong to the previous categories:

  • BooleanField()

  • FileField: (upload_to = "path", max_length="250"): This field is used to store files on the server

  • ImageField(upload_to = "path", max_length="250", height_field =height_img, width_field= width_img): This field corresponds to FileField but imparts special treatment to images such as storing the image's height and width

The descriptions of the options are as follows:

  • Upload_to: This defines the folder that will store the files corresponding to this field.

  • max_length: The FileField and ImageField fields are actually text fields that store the path and name of the uploaded file.

  • height_field and width_field: These take an integer field of the model as an argument. This field is used to store the size of the image.

Relationship between models

This section contains the types of fields that define the relationships between models:

  • ForeignKey (model, related_name = "foreign_key_for_dev", to_field="field_name", limit_choices_to=dict_or_Q, on_delete=)

  • OneToOneField (model, related_name = "foreign_key_for_dev", to_field="field_name", limit_choices_to=dict_or_Q, on_delete=)

  • ManyToManyField (model, related_name = "foreign_key_for_dev", to_field="field_name", limit_choices_to=dict_or_Q, on_delete=)

The descriptions of the options are as follows:

  • model: Here, you must specify the name of the model class you want to use.

  • related_name: This allows you to name the relationship. It is essential when multiple relationships to the same model exist.

  • to_field: This defines a relationship to a specific field of the model. By default, Django creates a relationship to the primary key.

  • on_delete: The database action on the removal of a field can be CASCADE, PROTECT, SET_NULL, SET_DEFAULT, and DO_NOTHING.

  • limit_choices_to: This defines the queryset that restricts records for the relationship.

The model meta attributes

The model meta attributes are to be defined in a meta class in the model in the following way:

class Product(models.Model):
  name = models.CharField()
  class Meta:
    verbose_name = "product"

The following attributes are used to define information about the model in which they are placed:

  • db_tables: This sets the name of the table stored in the database

  • verbose_name: This sets the name of a record for the user

  • verbose_name_plural: This sets the name of several records for the user

  • ordering: This sets a default order when listing records

Options common to all fields of models

The following options are common to all the fields of a model:

  • default: This sets a default value for the field.

  • null: This enables the null value for the field and makes an optional relationship if this option is defined on a relationship field.

  • blank: This enables you to leave the field empty.

  • error_messages: This specifies a series of error messages.

  • help_text: This sets a help message.

  • unique: This defines a field that does not contain duplicates.

  • verbose_name: This defines a field name that is readable by a human. Do not put a capital letter first; Django will do it automatically.

  • choices: This defines the number of possible choices for the field.

  • db_column: This sets the name of the field created in the database.