feat: Dear Diary Project - 6. Create Entry Model

master
Bobson Lin 5 years ago
parent 37d7fbec4c
commit bf69142fc0

@ -24,10 +24,10 @@ App Files
-----
* migrations - For database versioning.
* admin.py - Register models appearing at admin dashboard.
* models.py - Control whats save in your database.
* tests.py - Tests for this app
* views.py - (Render views/Response) for url
* [admin.py](./entries/admin.py) - Register models appearing at admin dashboard.
* [models.py](./entries/models.py) - Control whats save in your database.
* [tests.py](./entries/tests.py) - Tests for this app
* [views.py](./entries/views.py) - (Render views/Response) for url
Admin Dashboard
@ -87,13 +87,32 @@ Admin Dashboard
Display Templates
-----
1. Create templates directory for `entries` app
2. Create functions(index, add) in views.py
3. Create and adjust urls.py
4. Add `entries` app in settings.py `INSTALLED_APPS` section
2. Create functions(index, add) in [views.py](./entries/views.py)
3. Create [entries/urls.py](./entries/urls.py) and adjust [dear_diary/urls.py](./dear_diary/urls.py)
4. Add `entries` app in [settings.py](./dear_diary/settings.py) `INSTALLED_APPS` section
Add Page Link
-----
1. Use `name` argument in `django.urls.path` function.
2. In template file(*.html), add `{% url '[name]' %}` (eg. `{% url 'add' %}` for `/add` url)
Create Entry Model
-----
1. Add Entry model class in [models.py](./entries/models.py)
2. Register Entry model in [admin.py](./entries/admin.py)
3. Run command to create migration file and do migration.
```
$ python manage.py makemigrations
Migrations for 'entries':
entries/migrations/0001_initial.py
- Create model Entry
$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, entries, sessions
Running migrations:
Applying entries.0001_initial... OK
```
4. Check http://127.0.0.1:8000/admin

@ -1,3 +1,5 @@
from django.contrib import admin
# Register your models here.
from .models import Entry
admin.site.register(Entry)

@ -0,0 +1,25 @@
# Generated by Django 2.2.2 on 2019-06-24 03:24
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Entry',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('text', models.TextField()),
('date', models.DateTimeField(auto_now_add=True)),
],
options={
'verbose_name_plural': 'Entries',
},
),
]

@ -1,3 +1,11 @@
from django.db import models
# Create your models here.
class Entry(models.Model):
text = models.TextField()
date = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name_plural = 'Entries'
def __str__(self):
return 'Entry #{}'.format(self.id)

Loading…
Cancel
Save