All technological notes.
model:
django.db.models.Model and each field of the model class represents a database field (column).Model is defined in models.py file of each application package.
Primary key fields
Each model requires exactly one field to have primary_key=True (either explicitly declared or automatically added).
primary_key=True on one of fields.Field.primary_key, django won’t add the automatic id column.SQL
Table Name:
CREATE TABLE SQL:
objects attribure:
Manager instance, the interface through which Django models take database query operations.Model Methods
Migration:
Django creates a migration file inside the migration folder for each model to create the table schema, and each table is mapped to the model of which migration is created.
Command to perform migration:
| Command | Description |
|---|---|
makemigrations |
create a migration py file for the tabled schema of a model. |
migrate |
create table according to the schema defined in the migration file. |
sqlmigrate |
show a raw SQL query of the applied migration. |
showmigrations |
lists out all the migrations and their status. |
models.pyCreate a new class which inherits from models.Model
<app_name> / models.py:
from django.db import models
class Employee(models.Model):
first_name = models.CharField(max_length=32)
last_name = models.CharField(max_length=32)
email = models.EmailField()
settings.pyRegister model into the INSTALLED_APPS inside settings.py.
settings.py:
INSTALLED_APPS = [
#...
'app_name',
#...
]
makemigrationsCreates new migrations based on the changes detected to models.
CLI:
# create migration files for all apps
$ py manage.py makemigrations
# create migrations files for a specific app
$ py manage.py makemigrations app_label1 app_label2 ...
migratemigrate
$ py manage.py migrate [app_label] [migration_name]
showmigrationsshowmigrations
CLI:
# show migrations for all apps
$ py manage.py showmigrations
# show migrations for specific apps
$ py manage.py showmigrations [app_label]
# return:
# EmpApp
# [X] 0001_initial
sqlmigrate sqlmigrate
$ py manage.py sqlmigrate app_label migration_name
# app_label: the name of current application
# migration_name: the name of migration file
# example:
$ py manage.py sqlmigrate EmpApp 0001
# return:
# BEGIN;
# --
# -- Create model Employee
# --
# CREATE TABLE "EmpApp_employee" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "first_name" varchar(32) NOT NULL, "last_name" varchar(32) NOT NULL, "email" varchar(254) NOT NULL);
# COMMIT;
Model Classdjango.db.models.Model| Exception | Description |
|---|---|
Model.DoesNotExist |
when an expected object is not found |
Model.MultipleObjectsReturned |
when multiple objects are found |
| Attributes | Description |
|---|---|
Model.objects |
the default name of the Manager |
| Method | Description |
|---|---|
Model(**kwargs) |
instantiatie a model without db |
| Method | Description |
|---|---|
refresh_from_db() |
reload a model’s values from the database |
get_deferred_fields() |
return the attribute names of all fields |
| Method | Description |
|---|---|
clean_fields() |
validate all fields on model |
clean() |
custom model validation |
validate_unique() |
validates uniqueness constraints |
validate_constraints() |
validates all constraints |
full_clean() |
call the above methods |
| Method | Description |
|---|---|
save() |
insert / udpate an object back to the database |
delete() |
deletes the object in the database |
| Method | Description |
|---|---|
__str__() |
display a model instance |
__eq__() |
compare primary key value |
get_absolute_url() |
return a HTTP string to refer to the object |
get_field_name_display() |
returns the “human-readable” value of the choice field |