# django learn from docs personal note ###### tags: `python` `django` `backend` `learn` `project` source code: https://github.com/caoqianming/django-vue-admin 後端程式心智圖:https://gitmind.com/app/doc/d6a2710078 資料庫連結關係圖: https://dbdiagram.io/d/60ddab06dd6a5971482828d7 drf view 資料 ### work flow ``` source /home/lj/Documents/GitHub/djangoLearnFromDocs/venv/bin/activate python manage.py runserver python manage.py shell ``` #### test work flow ``` 1. coverage run --omit='*/venv/*' manage.py test coverage html 2. coverage run -m pytest 3. pytest --cov=myproj tests/ pytest --cov= tests/ ``` github: https://github.com/LJlkdskdjflsa/djangoLearnFromDocs ## pytest-django: note: https://hackmd.io/IYLAHhIiSaaeg5XrBASkUA ### qrcode ``` pip install qrcode ``` https://towardsdatascience.com/generate-qrcode-with-python-in-5-lines-42eda283f325 ### test coverage ``` pip install coverage ``` run test ``` coverage run -m pytest coverage report -m coverage run --omit='*/venv/*' manage.py test coverage html ``` pytest + coverage https://pytest-cov.readthedocs.io/en/latest/xdist.html ### tutorail ### cors https://github.com/adamchainz/django-cors-headers ``` pip install django-cors-headers ``` ### view set problems As the project scaled - increase amount of view - Endpoints subsequently scales with views - Repetition of code in the views (queryset) - Multiple endpoint (urls) can be difficult to maintain viewset - easy to write code - additional layer of abstraction - repeated logic can be combined into a single calss - combine logic for a set of related views in a single classes - write less code (DRY) ### router - auto generate url patterns for developer - routes for create/retrieve/update/destroy style action ### api docs and schema ``` pip install pyyaml pip install uritemplate ``` core api (vs open api) (use just in DRF) ``` pip install coreapi ``` ### make search DRF view info https://www.cdrf.co/ 1. base on user 2. base on url ``` pip install django-filter ``` https://django-filter.readthedocs.io/en/stable/guide/install.html ### upload image ``` pip install pillow ``` ### social login https://github.com/wagnerdelima/drf-social-oauth2 ``` pip install drf_social_oauth2 pip uninstall djangorestframework-simplejwt client id:UovwEV9Ek5oTmvjXzmj5tBAUaYbS6TlXd0bHcnfw client secret:M73NoRXxREEgy1QPJg7RQXQWldopvLLATtnFwqL17rYn1TwGgF1MsfwSabs1j4YZ8KBFoRmWUFPGBs2DlXE6Jex7A2YB6BBAaaA2e7YCWumhEfAlLAt3AnFfE3DNsiyX ``` test retrive token ``` curl -X POST -d "client_id=UovwEV9Ek5oTmvjXzmj5tBAUaYbS6TlXd0bHcnfw&client_secret=M73NoRXxREEgy1QPJg7RQXQWldopvLLATtnFwqL17rYn1TwGgF1MsfwSabs1j4YZ8KBFoRmWUFPGBs2DlXE6Jex7A2YB6BBAaaA2e7YCWumhEfAlLAt3AnFfE3DNsiyX&grant_type=password&username=admin@admin.com&password=admin" http://localhost:8000/auth/token ``` return ``` {"access_token":"HknQt6LQijZtxN8Vv4dDRk6mGedkxI","expires_in":36000,"token_type":"Bearer","scope":"read write","refresh_token":"qOQgn0rLbfj3ZxC6qh9pgwL7LnY5ij"} ``` ### Admin video source : https://www.youtube.com/watch?v=djHsh4V79Yo&t=20s&ab_channel=VeryAcademy #### make own custom admin Q: Select a valid choice. That choice is not one of the available choices. ### project todo: Import djongo DB engine make the non static things keep learning pytest fix test_create_things ### 遷移到mongodb error ``` djongo.exceptions.SQLDecodeError: Keyword: Unknown token: TYPE Sub SQL: None FAILED SQL: ('ALTER TABLE "social_auth_association" ALTER COLUMN "id" TYPE long',) Params: ([],) Version: 1.3.6 The above exception was the direct cause of the following exception: ``` example: https://bezkoder.com/django-mongodb-crud-rest-framework/ ### deploy 為了自己建social login