Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

Django IntroductionChapter 3. Django Models and Databases

Django Models – Data Field Type

Django Models – Data Field Type

Django Models: Understanding Data Field Types

Fields are a crucial part of a relational database storing data. Each field determines a data type that can store a specific type of data. Django provides several field types. A field type is written in PascalCase. Some field types require specific arguments. For example, max_length is required for CharField. In this section, we'll explain key data field types.

Field types for strings

CharField

This field type is one of the most frequently used field types. This field can store small-to-large strings.

Key Arguments
  • max_length: Required argument. Without this argument, Django gives an error message. Using this argument, you can specify the maximum length of the field.

TextField

This field is used to store a large text.

Key Arguments
  • max_length: Optional argument. Using this argument, you can specify the maximum length of the field.

SlugField

This field is used to store strings of alphabets, numbers, underscores, or hyphens, which are generally used in URLs.

Key Arguments
  • max_length: Optional argument. If you don't specify max_length, a default of 50 is used.

URLField

This field is used to store a URL validated by a URL validator.

Key Arguments
  • max_length: Optional argument. If you don't specify max_length, a default of 200 is used.

Field types for Logic

BooleanField

This field is used to store a True or False value. In a form input, this field usually becomes a check box input.

3. Field types for Numbers

IntegerField

This field is used to store an integer. According to the Django documentation, Values from -2147483648 to 2147483647 are safe in all databases supported by Django.

PositiveIntegerField

This field is used to store a positive integer or zero value. According to the Django documentation, Values from 0 to 2147483647 are safe in all databases supported by Django.

FloatField

This field is used to store a floating-point number represented in Python by a float instance.

DecimalField

This field is used to store a fixed-precision decimal number, represented in Python by a decimal instance.

Key Arguments
  • decimal_places: Required argument. Using this argument, you can specify the number of decimal places to store the number.
  • max_digits: Required argument. Using this argument, you can specify the maximum number of digits allowed in the number. This number must be greater than or equal to decimal_places.

IdeaNote: Floating-point number vs. decimal number

Floating-point numbers in FloatField are handled in binary numbers. In some cases, calculations using this field may not be very accurate, but the data processing speed tends to be faster.

Decimal numbers in DecimalField are handled in decimal numbers. Calculations using this field are more accurate, but the data processing speed tends to be slower.

AutoField

This field is used to store an integer that automatically increments. This type of field is used for primary key IDs. Django gives each model an auto-incrementing primary key by default, so you usually won’t need to use this field type.

4. Field types for Date & Time

DateField

This field is used to store a date, represented in Python by a datetime.date instance.

Key Arguments
  • auto_now_add: Optional argument. This option is often used for the created_at column in a database table. When this argument is True, the value in the field is set with the current date when the record object is first created.
  • auto_now: Optional argument. This option is often used for the updated_at column in a database table. When this argument is True, the value in the field is updated with the current date when the record object is updated and saved.

DateTimeField

This field is used to store date and time, represented in Python by a datetime.datetime instance.

Key Arguments

The auto_now_add and auto_now options are optional arguments for this field type.

TimeField

This field is used to store time, represented in Python by a datetime.time instance.

Key Arguments

The auto_now_add and auto_now options are optional arguments for this field type.

IdeaTips: Editable timestamp

When auto_now_add or auto_now argument is True, you won't be able to modify the field (DateField or DateTimeField) as it is used for the auto-generated timestamp. If you want to modify the field, you need to try another implementation using imported modules like the ones below.

  • For DateField: default=date.today - from datetime.date.today()
  • For DateTimeField: default=timezone.now - from django.utils.timezone.now()

We'll explain this in the later section of this chapter.

5. Field types for Files

ImageField

This field is used to store image file information. The Pillow library is required to use this field type.

Key Arguments
  • upload_to: Required argument. Using this argument, you can specify the location (subdirectory) to save an uploaded image file. This argument only specifies a subdirectory path. The main directory for uploading files is defined in the settings.py file – MEDIA_ROOT and MEDIA_URL. If you are not creating a subdirectory, you can use a blank for the option value (upload_to=' '). We'll explain how to edit settings.py for media file handling later.
  • height_field: Optional argument. Using this argument, you can specify the height of the image.
  • width_field: Optional argument. Using this argument, you can specify the width of the image.

IdeaNote: Pillow

Pillow is a Python Imaging Library (PIL) fork that adds image processing capabilities to the Python interpreter. When you use ImageField in Django, you need this library in your app.

FileField

This field is used to store file information. Using this field type, you can handle other-than-image files.

Key Arguments

There are optional arguments for this field type, such as upload_to and storage.


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Django Templates vs. Django APIs for Web Development

Django Templates vs. Django APIs

Key Steps for Deploying a Django App

Key Steps of Django App Deployment

Managing Static Files in Django with {% static %}

Static Files in Development Environment – {% static %} tag

Understanding the Difference Between Django Projects and Apps

Project vs. App

Django Templates vs. Django APIs for Web Development

Django Templates vs. Django APIs

Key Steps for Deploying a Django App

Key Steps of Django App Deployment

Managing Static Files in Django with {% static %}

Static Files in Development Environment – {% static %} tag

Understanding the Difference Between Django Projects and Apps

Project vs. App

Tags:

PascalCase

Django Models

Relational Database

Data Field Type

Django Introduction
Course Content

Chapter 1. Django Key Concepts

Web Framework and Django

Websites vs. Django Web Apps

How Django Handles HTTP Request and HTTP Response

Django's MVT Framework

Django Templates vs. Django APIs

Chapter 2. Django Quick Start Guide

Install Python

Install Visual Studio Code

Create Project Directory

Set Up Virtual Environment

Install Django

Start Django Project

Run Server

Database Migration

URL dispatcher – urls.py

Create Superuser and Log In to Django Admin

Start App

Create HTML Templates

Create Views

Add URL Patterns

Project vs. App

Chapter 3. Django Models and Databases

Create a Database in Django

Relational Database

Create Django Models

Makemigrations and Migrate

Add Models in Django Admin – admin.py

Change Display Name of Record Objects

Django Models – Data Field Type

Django Models – Field Options

Django Models – Help Text Option

Django Models – Choices Option

Django Models – DateField with datetime Module

Django Models – Relationship Fields

Django Models – ID

Django Models – ForeignKey (OneToMany Relationship)

Django Models – OneToOneField

Django Models – ManyToManyField

Chapter 4. Create CRUD Web Application

CRUD Web Application

Basic CRUD Structure in Django

Django Generic Views

How To Write Class-Based Views with Generic Views

Generic View Basic Attributes

URL Dispatcher for CRUD Views

Django Templates for CRUD Views

Django Template Language (DTL)

Template for List Page

get_FOO_display method

Template for Detail Page

Template with Model Relations

Template for Create and Update Page

Template for Delete Page

Add Links – {% url %} tag

Extend Templates – {% extends %} tag

Check Developing App UI on Mobile Device

Django Templates with Bootstrap

Crispy Forms

Customize Views (1) – Change List Order

Customizing Views (2) – Filter Lists

Context

Customize Views (3) – Add Extra Context

Modularize Templates – {% include %} tag

Static Files in Development Environment – {% static %} tag

STATIC_URL and STATICFILES_DIRS

Create Index HTML

Chapter 5. User Management

User Authentication

Overview of User Management Functions

User Management Function Development with Django

Approaches to Building User Management Functions in Django

Django Allauth (1) – Introduction

Django Allauth (2) – Installation and Initial Settings

Django Allauth (3) – Email Verification via Console

Django Allauth (4) – Email Verification via Gmail

Django Allauth (5) – Social Login with GitHub

Django Allauth (6) – Social Login with Google

Django Allauth (7) – Allauth Template File Setup

Django Allauth (8) – Add Basic Styling with Bootstrap and Crispy Forms

Django Allauth (9) – Customize Sign-in and Sign-up Pages

User Models

Login Required – LoginRequiredMixin

User Login Status Icon on Navigation Bar

Chapter 6. Deploy Django App

Overview of Django App Deployment (1)

Overview of Django App Deployment (2)

Key Steps of Django App Deployment

Hosting Service Initial Settings (1) – AWS Lightsail setup

Hosting Service Initial Settings (2) – SSH Remote Connection

Manage Local Computer and Remote Server Simultaneously

Tips for Managing Local Development and Remote Production Environment

Hosting Service Initial Settings (3) – Clone Project Directory with GitHub

Production Database Setup

Django Production Settings (1) – Settings.py for Development and Production

Django Production Settings (2) – Production Settings

Django Production Settings (3) – django-environ and .env file

Static File Settings

Django and Dependency Installation on Production Server

Web Server and Application Server in Django

Application Server Setup – Gunicorn

Web Server Setup – Nginx

Domain Setup

SSL Setup – Certbot

Email Setting – SendGrid

Social Login for Production

Manage Local Development and Remote Production Environment

Chapter 1. Django Key Concepts

Web Framework and Django

Websites vs. Django Web Apps

How Django Handles HTTP Request and HTTP Response

Django's MVT Framework

Django Templates vs. Django APIs

Chapter 2. Django Quick Start Guide

Install Python

Install Visual Studio Code

Create Project Directory

Set Up Virtual Environment

Install Django

Start Django Project

Run Server

Database Migration

URL dispatcher – urls.py

Create Superuser and Log In to Django Admin

Start App

Create HTML Templates

Create Views

Add URL Patterns

Project vs. App

Chapter 3. Django Models and Databases

Create a Database in Django

Relational Database

Create Django Models

Makemigrations and Migrate

Add Models in Django Admin – admin.py

Change Display Name of Record Objects

Django Models – Data Field Type

Django Models – Field Options

Django Models – Help Text Option

Django Models – Choices Option

Django Models – DateField with datetime Module

Django Models – Relationship Fields

Django Models – ID

Django Models – ForeignKey (OneToMany Relationship)

Django Models – OneToOneField

Django Models – ManyToManyField

Chapter 4. Create CRUD Web Application

CRUD Web Application

Basic CRUD Structure in Django

Django Generic Views

How To Write Class-Based Views with Generic Views

Generic View Basic Attributes

URL Dispatcher for CRUD Views

Django Templates for CRUD Views

Django Template Language (DTL)

Template for List Page

get_FOO_display method

Template for Detail Page

Template with Model Relations

Template for Create and Update Page

Template for Delete Page

Add Links – {% url %} tag

Extend Templates – {% extends %} tag

Check Developing App UI on Mobile Device

Django Templates with Bootstrap

Crispy Forms

Customize Views (1) – Change List Order

Customizing Views (2) – Filter Lists

Context

Customize Views (3) – Add Extra Context

Modularize Templates – {% include %} tag

Static Files in Development Environment – {% static %} tag

STATIC_URL and STATICFILES_DIRS

Create Index HTML

Chapter 5. User Management

User Authentication

Overview of User Management Functions

User Management Function Development with Django

Approaches to Building User Management Functions in Django

Django Allauth (1) – Introduction

Django Allauth (2) – Installation and Initial Settings

Django Allauth (3) – Email Verification via Console

Django Allauth (4) – Email Verification via Gmail

Django Allauth (5) – Social Login with GitHub

Django Allauth (6) – Social Login with Google

Django Allauth (7) – Allauth Template File Setup

Django Allauth (8) – Add Basic Styling with Bootstrap and Crispy Forms

Django Allauth (9) – Customize Sign-in and Sign-up Pages

User Models

Login Required – LoginRequiredMixin

User Login Status Icon on Navigation Bar

Chapter 6. Deploy Django App

Overview of Django App Deployment (1)

Overview of Django App Deployment (2)

Key Steps of Django App Deployment

Hosting Service Initial Settings (1) – AWS Lightsail setup

Hosting Service Initial Settings (2) – SSH Remote Connection

Manage Local Computer and Remote Server Simultaneously

Tips for Managing Local Development and Remote Production Environment

Hosting Service Initial Settings (3) – Clone Project Directory with GitHub

Production Database Setup

Django Production Settings (1) – Settings.py for Development and Production

Django Production Settings (2) – Production Settings

Django Production Settings (3) – django-environ and .env file

Static File Settings

Django and Dependency Installation on Production Server

Web Server and Application Server in Django

Application Server Setup – Gunicorn

Web Server Setup – Nginx

Domain Setup

SSL Setup – Certbot

Email Setting – SendGrid

Social Login for Production

Manage Local Development and Remote Production Environment