March 2025
Intermediate to advanced
304 pages
4h 33m
Japanese
SQLAlchemyやFlaskの代わりにDjangoを使いたかったとする。どうなるだろう? まず第一に、インストールする場所を選ぶことだ。 私たちは Django を、メインのアロケーションコードの隣に、別のパッケージとして置く:
├──src│├──allocation││├──__init__.py││├──adapters│││├──__init__.py...│├──djangoproject││├──alloc│││├──__init__.py│││├──apps.py│││├──migrations││││├──0001_initial.py││││└──__init__.py│││├──models.py│││└──views.py││├──django_project│││├──__init__.py│││├──settings.py│││├──urls.py│││└──wsgi.py││└──manage.py│└──setup.py└──tests├──conftest.py├──e2e│└──test_api.py├──integration│├──test_repository.py...
この付録のコードはGitHubのappendix_djangoブランチにある:
git clone https://github.com/cosmicpython/code.git cd code git checkout appendix_django
というプラグインを使用した。pytest-djangoというプラグインを使用した。
Django ORM/QuerySet 言語の呼び出しで生の SQL を書き換えるだけです:
最初のリポジトリテストを適応した (tests/integration/test_repository.py)
fromdjangoproject.allocimportmodelsasdjango_models@pytest.mark.django_dbdeftest_repository_can_save_a_batch():batch=model.Batch("batch1","RUSTY-SOAPDISH",100,eta=date(2011,12,25))repo=repository.DjangoRepository()repo.add(batch)[saved_batch]=django_models.Batch.objects.all()assertsaved_batch.reference==batch.referenceassertsaved_batch.sku==batch.skuassertsaved_batch.qty==batch._purchased_quantityassert ...
Read now
Unlock full access