はじめに
Pythonの経験がほぼゼロの状態から、
なにかと評判の高いDjangoというWebアプリケーション開発のフレームワークを触ってみました。
これまでに、Ruby on Rails, Nest.js, ASP.Net Core, Laravelあたりを触ったことがあるため、それらとなんとなく比較しながら、個人的にいいなと思った部分のみまとめます。
触ったもの
公式チュートリアル を一通りやってみました。
チュートリアルの内容
- Part 1: Requests and responses
- Part 2: Models and the admin site
- Part 3: Views and templates
- Part 4: Forms and generic views
- Part 5: Testing
- Part 6: Static files
- Part 7: Customizing the admin site
- Advanced Tutorials: How to write reusable apps
- Advanced Tutorials: Writing your first patch for Django
チュートリアルを終えたソースコード
いいなと思ったこと
CLIのコマンドが豊富
Djangoが有効になっているPython仮装環境下において、
python manage.py {コマンド名}
で様々なコマンドを実行することができます。
有効なコマンドの例としては、
-
runserver: サーバーの起動 -
test: テストの実行 -
check: ソースコードに問題がないかどうかチェック -
shell:rails consoleのような感じで、REPL環境でdjangoのモデルなどをいじれる -
makemigration/migrate: マイグレーション関連 -
sqlmigrate: マイグレーションが実行する予定のSQLをプリントして確認できる -
createsuperuser: シェル上でインタラクティブにユーザー名・パスワードなどを入力するでユーザーが作れる。初期ユーザーの生成などに便利そう
外部キーでリレーションを表現
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
この例だと、Choiceモデルに外部キーとして question を設定することでそれに基づいてリレーションが作られます。
外部キー制約の貼り忘れがなくていいなと思いました。
管理画面
これが個人的に1番いいなと思いました。
はじめは、「ブログサービスを作るわけでもないのに管理画面なんていらんやろ」ぐらいに思っていましたが、
上図のように、UI上で様々なモデルインスタンスの新規作成・編集などを簡単に行うことができます。
この例のように、Question has many Choices のようなリレーション関係の時に、
Question の生成画面で紐づく Choiceインスタンスの生成も同時に生成できるようなUIのカスタマイズが非常に簡単で、素晴らしかったです。
テストが楽
たとえば、モデルのテストであれば、
import datetime
from django.test import TestCase
from django.utils import timezone
from .models import Question
class QuestionModelTests(TestCase):
def test_was_published_recently_with_future_question(self):
"""
was_published_recently() returns False for questions whose pub_date
is in the future.
"""
time = timezone.now() + datetime.timedelta(days=30)
future_question = Question(pub_date=time) # テスト用のモデルインスタンスの生成
self.assertIs(future_question.was_published_recently(), False) # インスタンスのプロパティにアクセスしてアサーション
Viewのテストであれば
# テスト用のヘルパー関数
def create_question(question_text, days):
"""
Create a question with the given `question_text` and published the
given number of `days` offset to now (negative for questions published
in the past, positive for questions that have yet to be published).
"""
time = timezone.now() + datetime.timedelta(days=days)
return Question.objects.create(question_text=question_text, pub_date=time)
class QuestionIndexViewTests(TestCase):
def test_no_questions(self):
"""
If no questions exist, an appropriate message is displayed.
"""
response = self.client.get(reverse('polls:index')) # `/polls` にリクエストを投げる
self.assertEqual(response.status_code, 200) # ステータスコードのチェック
self.assertContains(response, "No polls are available.") # レンダーされたレスポンステキストのチェック
self.assertQuerysetEqual(response.context['latest_question_list'], []) # コントローラーがテンプレートに渡している変数の中身のチェック
いずれにおいてもテストの実行時には、
- テスト用のDBを生成
- 各テストの実行前にDBを綺麗な状態に戻す
- 各テストを実行する
ということを自動でやってくれます。
db cleaner的なものを自前で実装しなければならないフレームワークも結構多いので、これはかなり好印象でした。
まとめ
「ん?これはどうなんだろう?」という部分がなかったわけでもないので、
それもまとめようかと検討したのですが、とはいえ実務でまだ使ったことがないので無知を晒しかねず、この記事ではポジティブな部分のみに留めました。
チュートリアルの所要時間は2時間程度?なので興味の湧いた方はぜひやってみましょう!
