投稿時間:2023-04-08 13:09:12 RSSフィード2023-04-08 13:00 分まとめ(11件)

カテゴリー等 サイト名等 記事タイトル・トレンドワード等 リンクURL 頻出ワード・要約等/検索ボリューム 登録日
IT 気になる、記になる… Microsoft、「Windows 11 Insider Preview Build 25336」をCanaryチャネル向けにリリース https://taisy0.com/2023/04/08/170481.html build 2023-04-08 03:10:34
IT 気になる、記になる… Microsoft、「Windows 11 Insider Preview Build 23430」をDevチャネル向けにリリース https://taisy0.com/2023/04/08/170478.html build 2023-04-08 03:06:18
TECH Techable(テッカブル) 子どもに持たせても安全!キズや衝撃に強いiPad専用衝撃吸収ケース登場 https://techable.jp/archives/202736 pdaipadbk 2023-04-08 03:00:34
python Pythonタグが付けられた新着投稿 - Qiita 単体テスト工程で自動化コード実装時に意識している2つのこと https://qiita.com/affine92325659/items/90b240083d6aa4852789 pythonpyt 2023-04-08 12:25:09
Linux Ubuntuタグが付けられた新着投稿 - Qiita OVS+namespaceで仮想的なネットワークを構築してみる https://qiita.com/ohtsuka-shota/items/f5d7c32c7d6443c91f4e namespace 2023-04-08 12:03:31
Docker dockerタグが付けられた新着投稿 - Qiita OVS+namespaceで仮想的なネットワークを構築してみる https://qiita.com/ohtsuka-shota/items/f5d7c32c7d6443c91f4e namespace 2023-04-08 12:03:31
Azure Azureタグが付けられた新着投稿 - Qiita Azure で Ubuntu / Apache2 で Web サーバを公開するまで https://qiita.com/nanbuwks/items/bb37057277cc8691a307 ubuntu 2023-04-08 12:35:38
海外TECH DEV Community Getting started with Django Rest Framework https://dev.to/highcenburg/getting-started-with-django-rest-framework-3ma8 Getting started with Django Rest FrameworkPhoto by Markus Spiske In this article we ll create a project that posts about rants using Django and Django Rest Framework We ll use Django s built in slugify method and override its save method to automatically create a slug for each rant We ll also use a third party package called drf writable nested to handle the serialization of a ManyToManyField in the model We ll start by creating a virtual environment installing the initial packages creating the django project creating the django app and finally doing the initial migrationspython m venv venv venv bin activatepython m pip install django djangorestframeworkdjango admin startproject myprojectcd myprojectpython m manage startapp rantspython m manage migrateWe list rest framework and our rants app in the INSTALLED APPS settings of our project and include them in the project urls py settings pyINSTALLED APPS rest framework rants myproject urls pyfrom django urls import path includeurlpatterns path include rants urls namespace main path api auth include rest framework urls namespace rest framework Next we create the models inside the rants app models pyfrom django db import modelsclass Category models Model title models CharField max length slug models SlugField max length def str self return self titleclass Rant models Model title models CharField max length slug models SlugField max length categories models ManyToManyField Category related name rants categories class Meta verbose name rant verbose name plural rants def str self return self titleWe have a Rant model with a title slug with a CharField and a categories attribute with a ManyToManyField connected to a Category model with a title and a slug attribute Then we migrate the database python m manage makemigrations amp amp python m manage migrate Next we create a serializer for both models serializers pyfrom rest framework import serializersfrom models import Rant Categoryclass CategorySerializer serializers ModelSerializer slug serializers SlugField read only True class Meta model Category fields all class RantSerializer serializers ModelSerializer categories CategorySerializer many True slug serializers SlugField read only True class Meta model Rant fields id title slug categories many TrueFinally we create our views and map it to our urls so we can see the API endpoints of our app views pyfrom rest framework response import Responsefrom rest framework generics import ListCreateAPIView UpdateAPIView DestroyAPIViewfrom models import Rantfrom serializers import RantSerializerclass RantList ListCreateAPIView queryset Rant objects all serializer class RantSerializer def list self request queryset self get queryset serializer RantSerializer queryset many True return Response serializer data class RantUpdate UpdateAPIView queryset Rant objects all serializer class RantSerializerclass RantDelete DestroyAPIView queryset Rant objects all serializer class RantSerializerWe use ListCreateAPIView for read write endpoints to represent a collection of model instances which provide a get and post method handler UpdateAPIView for update only endpoints of a single model instance which provides a put and patch method handler DestroyAPIView for a delete only endpoint of a single model instance which provides a delete method handler Let s map these views to the urls py urls pyfrom django urls import pathfrom views import RantList RantUpdate RantDeletefrom models import Rantfrom serializers import RantSerializerapp name rants urlpatterns path api rants RantList as view queryset Rant objects all serializer class RantSerializer path api rants update lt int pk gt RantUpdate as view queryset Rant objects all serializer class RantSerializer path api rants delete lt int pk gt RantDelete as view queryset Rant objects all serializer class RantSerializer We can now view the api endpoints in the browser using the drf package but I personally prefer to see the api endpoints using another package which is the drf yasg package Let s install and configure the package python m pip install drf yasg settings pyINSTALLED APPS rest framework drf yasg urls pyfrom django urls import path includefrom rest framework import permissionsfrom drf yasg views import get schema viewfrom drf yasg import openapischema view get schema view openapi Info title Rants API default version v description Rants terms of service contact openapi Contact email contact snippets local license openapi License name BSD License public True permission classes permissions AllowAny urlpatterns path api schema view with ui swagger cache timeout name schema swagger ui Now we run python m manage runserver and head over to http localhost api to see what we ve createdBut we have a problem the categories field has an error despite inputting a str in the field categories Expected a list of items but got type str To solve this we ll install drf nested writable in our app to serialize the categories field then update the serializers py file to include WritableNestedModelSerializer in our RantSerializerpython m pip install drf nested writable serializers pyfrom drf writable nested serializers import WritableNestedModelSerializerclass RantSerializer WritableNestedModelSerializer categories CategorySerializer many True slug serializers SlugField read only True class Meta model Rant fields id title slug categories many TrueNow when we add new data for our app using the rants create endpoint we won t get the error we got above anymore but instead We also override the save method in our models for the slug field to automatically fill the database inThe code to override the save method in our models models py from django utils text import slugify def save self args kwargs self slug slugify self title super Category self save args kwargs return self slug def save self args kwargs self slug slugify self title super Rant self save args kwargs return self slug Conclusion This took me a while to solve since I m at GMT but hey at least it got solved and I learned how to override the save method in the models and learned about the drf writable nested package to fix my issue 2023-04-08 03:47:53
海外科学 NYT > Science Texas Judge Invalidates FDA Approval of the Abortion Pill Mifepristone https://www.nytimes.com/2023/04/07/health/abortion-pills-ruling-texas.html Texas Judge Invalidates FDA Approval of the Abortion Pill MifepristoneThe Texas judge s ruling was quickly contradicted by another federal judge in Washington State who ordered the F D A to keep mifepristone available 2023-04-08 03:07:23
海外ニュース Japan Times latest articles Missing SDF chopper had normal radio communications before vanishing https://www.japantimes.co.jp/news/2023/04/08/national/sdf-military-helicopter-search/ Missing SDF chopper had normal radio communications before vanishingThe search continued Saturday as the hunt shifted its focus to the seabed with at least part of the helicopter thought to have sunk to 2023-04-08 12:34:32
ビジネス 東洋経済オンライン 母の死で「縁を切った兄弟」が久々に向き合えた訳 漫画「プラタナスの実」(第7集・第58話) | プラタナスの実 | 東洋経済オンライン https://toyokeizai.net/articles/-/659398?utm_source=rss&utm_medium=http&utm_campaign=link_back 東洋経済オンライン 2023-04-08 12:30:00

コメント

このブログの人気の投稿

投稿時間:2021-06-17 05:05:34 RSSフィード2021-06-17 05:00 分まとめ(1274件)

投稿時間:2021-06-20 02:06:12 RSSフィード2021-06-20 02:00 分まとめ(3871件)

投稿時間:2020-12-01 09:41:49 RSSフィード2020-12-01 09:00 分まとめ(69件)