18. 開発用サーバー起動
$ cd myproject
$ python manage.py runserver
System check identified no issues (0 silenced).
March 03, 2017 - 15:23:22
Django version 1.10.6, using settings 'stapydemo.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
27. 認証とセッション関連の
テーブルを作成
$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying sessions.0001_initial... OK
まずmigrateして、認証とセッション関連をDB上にテーブルを作成する
28. 管理者ユーザ作成
$ python manage.py createsuperuser
Username (leave blank to use 'nakazawa'): admin
Email address: admin@any.com
Password:
Password (again):
Superuser created successfully.
createsuperuser で管理者ユーザを作成する
29. テーブルの中身をのぞいてみる
$ python manage.py dbshell
SQLite version 3.8.10.2 2015-05-20 18:17:19
Enter ".help" for usage hints.
sqlite>
settings.pyに設定してあるDBエンジンに接続(デフォは、sqlite)
sqlite> .tables
auth_group auth_user_user_permissions
auth_group_permissions django_admin_log
auth_permission django_content_type
auth_user django_migrations
auth_user_groups django_session
30. SQLを叩いてみる
sqlite> .header on
sqlite> .mode column
sqlite> select id, username, email from auth_user;
id username email
---------- ---------- -------------
1 admin admin@any.com
31. from django.db import models
class Book(models.Model):
"""書籍"""
name = models.CharField('書籍名', max_length=255)
publisher = models.CharField('出版社', max_length=255, blank=True)
page = models.IntegerField('ページ数', blank=True, default=0)
def __str__(self):
return self.name
class Impression(models.Model):
"""感想"""
book = models.ForeignKey(Book, verbose_name='書籍', related_name='impressions')
comment = models.TextField('コメント', blank=True)
def __str__(self):
return self.comment
Modelの実装
「書籍」と各書籍の「感想」をモデル化(関連が1:nのモデル)