Model

Django Basic Flow

  1. Define url

    • urls.py is a signpost!

  2. Create function to execute in views.py

  3. Create html to return

Django project / app configuration

  • Django has a structure where one project has multiple apps

  • Each app has an MTV pattern

  • When composed of multiple apps, name duplication is possible, so structure as template/{app name}/{}.html

    • why?

      • Subdirectories of templates folder created in individual apps are used as template files (default)

      • Django follows the declaration order of DIR and INSTALLED_APPS in settings.py when searching for template files, so to prevent name duplication, create a folder with the same name as the app name under template

1. Project creation

2. Project basic configuration - settings.py

3. Create app (articles)

  • App names are generally composed in plural form

  • App registration

    settings.py

4. Create urls.py

Project folder

Individual app

Role of MTV

view

  • url

  • request (request related information)

  • return render()

Template

  • html

  • DTL

  • loop / conditional / filter

Model

  • DB

Model in Django

Model

: Single source of information about Data

Migrations

: A method to reflect model changes to the database schema

Migration Flow

  1. Model creation/modification/deletion etc.

  2. Create migration file

    • Migration file records model changes and consists of code to reflect them in the database

    • Think of migration file as a version control system for database schema! -> git

  3. Apply to database through migrate

Model
Migration
ORM (Query methods, QuerySet API)

Data management in MTV pattern

Reflect db schema defined by Model

Query statements that manipulate db (possible through Python object manipulation)

Model usage

1. Model definition

models.py

  • Create a class that inherits from models.Model

  • For attributes, specify the column names of the table you want to compose, and define fields according to data types

  • ID field is automatically created as pk value.

  • Field and option information defined above is as follows:

CharField vs TextField()

: Choose based on whether to receive data with <input> or <textarea> when actually receiving data with <form> tag

2. migration

Migrations are Django's way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema. Migration is a method for reflecting model changes to the database schema in django.

2-1. makemigrations

  • To reflect the defined model in the database, create a migration file through the migration command

  • Migration files record model changes and are recorded in the migrations/ folder for each app. Initially, a file called 0001_initial.py will be created.

  • Migration file manages model changes

    • Preparing to reflect modeled content in db!

2-2. migrate

  • Command to reflect created migration file to database

  • The reason it looks like a lot is because database migration files that django uses by default are also reflected

    • From now on, do python manage.py migrate as soon as you create a project!

Migration Flow

  • Create migration

  • Check migration DB reflection status

  • Output SQL statement corresponding to migration

  • Finally reflect migration file content to DB

+

admin registration

Django ORM

Basic database manipulation is called CRUD(Create, Read, Update, Delete) operation.

ORM (Object Relational Mapping)

  • Programming technique to convert incompatible data between DB and OOP language

    • ORM maps values stored in db to objects!

      • Manipulating db through Python object manipulation (method calls)!

Django shell

A function that allows you to use python interactive interpreter for django projects

  • Can be used conveniently through additional package installation.

  • After installation, add the following content to settings.py. (mind the comma)

  • And from now on, use the following command.

1. Create

2. Read

  • Read all data

  • Read single data

    Single data inquiry is possible through unique value id.

    • id == pk!

      • Does not allow duplicates

3. Update

  • To check if it was modified, query the data again

4. Delete

Using Admin Page

1. Create administrator account

2. admin registration

To use the admin page, it must be defined in admin.py for each app

3. Verification

  • Access /admin/ url and log in with administrator account

+

Tips

Fat Model

  • MVC

    • M (C) V

  • MTV

    • M (V) T

-> Make model fat!!!!

Last updated