Finished 03. Django models
parent
d609a518a9
commit
5a413f7e57
115
django-girls.md
115
django-girls.md
|
@ -64,3 +64,118 @@ python manage.py runserver
|
|||
|
||||
## 2. Django models
|
||||
|
||||
Target: create objects that store all posts in the blog.
|
||||
|
||||
### 2.1 Objects
|
||||
|
||||
In OOP, object containing member data and member function. We try to model the blog's objects as well.
|
||||
|
||||
Q:
|
||||
* How will we model blog posts?
|
||||
* What is a blog post?
|
||||
* What properties should it have?
|
||||
|
||||
A:
|
||||
* Blog posts need
|
||||
* text with content and title
|
||||
* author
|
||||
* date of created/published
|
||||
|
||||
Object's data can be shown as below:
|
||||
|
||||
```
|
||||
Post
|
||||
-------
|
||||
title
|
||||
text
|
||||
author
|
||||
created_date
|
||||
published_date
|
||||
```
|
||||
|
||||
Blog object's method:
|
||||
* `publish`
|
||||
|
||||
### 2.2 Django model
|
||||
|
||||
A model in Django is a special kind of obj, saved in `database`. Analogy: A model in database is like a spreadsheet with columns (fields) and rows (data).
|
||||
|
||||
#### 2.2.1 Creating and app
|
||||
|
||||
For organization, we create a separate app inside the web project, which contains logic and model.
|
||||
|
||||
```
|
||||
$ python manage.py startapp blog
|
||||
```
|
||||
|
||||
It created a dir called `/blog` that containing scripts:
|
||||
|
||||
```
|
||||
djangogirls
|
||||
├── blog
|
||||
│ ├── admin.py
|
||||
│ ├── apps.py
|
||||
│ ├── __init__.py
|
||||
│ ├── migrations
|
||||
│ │ └── __init__.py
|
||||
│ ├── models.py
|
||||
│ ├── tests.py
|
||||
│ └── views.py
|
||||
|...
|
||||
```
|
||||
|
||||
After creating app, `mysite/settings.py` should be changed to let Django notice the app.
|
||||
|
||||
```python
|
||||
INSTALLED_APPS = [
|
||||
'django.contrib.admin',
|
||||
'django.contrib.auth',
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
# first party
|
||||
'blog.apps.BlogConfig',
|
||||
]
|
||||
```
|
||||
|
||||
* `blog.apps.BlogConfig` is the class that in `blog/apps.py` script
|
||||
|
||||
#### 2.2.2 Creating a blog post model
|
||||
|
||||
For each app, there is a `models.py` that contains models (obj), modify it to contain post obj
|
||||
|
||||
```python
|
||||
from django.db import models
|
||||
from django.conf import settings
|
||||
from django.utils import timezone
|
||||
|
||||
class Post(models.Model):
|
||||
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
||||
title = models.CharField(max_length=200)
|
||||
text = models.TextField()
|
||||
created_date = models.DateTimeField(default=timezone.now)
|
||||
published_date = models.DateTimeField(blank=True, null=True)
|
||||
|
||||
def publish(self):
|
||||
self.published_date = timezone.now()
|
||||
self.save()
|
||||
|
||||
def __str__(self):
|
||||
return self.title
|
||||
```
|
||||
|
||||
Explaining Post class:
|
||||
* `models.Model` the base class the Post class inherit from. It means `Post` is a Django Model, so Django can save it in database
|
||||
* Properties are defined as Django's model data type:
|
||||
* `models.CharField`: A Django model class that define text with limited number of char
|
||||
* `models.TextField`: long text w/o limit
|
||||
* Check django models->fields->field-types
|
||||
|
||||
#### 2.2.3 Create tables for models in database
|
||||
|
||||
After creating new model, we need to add it into database:
|
||||
|
||||
1. Let Django know there is change: `python manage.py makemigrations blog`
|
||||
1. So, Django will prepare a migration file
|
||||
2. After prepared migration file `blog/migrations/0001_initial.py`, apply it to database: `python manage.py migrate blog`
|
|
@ -0,0 +1,3 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
|
@ -0,0 +1,5 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class BlogConfig(AppConfig):
|
||||
name = 'blog'
|
|
@ -0,0 +1,29 @@
|
|||
# Generated by Django 3.1.2 on 2020-10-19 04:23
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import django.utils.timezone
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Post',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('title', models.CharField(max_length=200)),
|
||||
('text', models.TextField()),
|
||||
('created_date', models.DateTimeField(default=django.utils.timezone.now)),
|
||||
('published_date', models.DateTimeField(blank=True, null=True)),
|
||||
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
]
|
|
@ -0,0 +1,17 @@
|
|||
from django.db import models
|
||||
from django.conf import settings
|
||||
from django.utils import timezone
|
||||
|
||||
class Post(models.Model):
|
||||
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
||||
title = models.CharField(max_length=200)
|
||||
text = models.TextField()
|
||||
created_date = models.DateTimeField(default=timezone.now)
|
||||
published_date = models.DateTimeField(blank=True, null=True)
|
||||
|
||||
def publish(self):
|
||||
self.published_date = timezone.now()
|
||||
self.save()
|
||||
|
||||
def __str__(self):
|
||||
return self.title
|
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
|
@ -0,0 +1,3 @@
|
|||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
BIN
src/db.sqlite3
BIN
src/db.sqlite3
Binary file not shown.
|
@ -38,6 +38,8 @@ INSTALLED_APPS = [
|
|||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
# first party
|
||||
'blog.apps.BlogConfig',
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
|
|
Loading…
Reference in New Issue