Model
Django Basic Flow
Define
urlurls.pyis a signpost!
Create function to execute in
views.pyCreate
htmlto 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}/{}.htmlwhy?
Subdirectories of templates folder created in individual apps are used as template files (default)
Django follows the declaration order of
DIRandINSTALLED_APPSinsettings.pywhen 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
settings.py
3. Create app (articles)
App names are generally composed in plural form
App registration
settings.py
4. Create urls.py
urls.pyProject 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
Model creation/modification/deletion etc.
Create
migrationfileMigration file records model changes and consists of code to reflect them in the database
Think of migration file as a
version control systemfor database schema! ->git
Apply to database through
migrate
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.ModelFor 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:max_length: Required
DateTimeFieldauto_now_add: (Optional) Automatically set the time value only when creatingauto_now: (Optional) Automatically set the time value every time it's modified
For other fields, check the link https://docs.djangoproject.com/ko/2.1/ref/models/fields/#field-types!
CharField vs TextField()
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 migrateas 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.
django-extensions basically provides useful functions for django development.
ipythonis installed to use interactive shell more conveniently
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