{"id":1517,"date":"2021-07-18T20:46:39","date_gmt":"2021-07-18T11:46:39","guid":{"rendered":"http:\/\/www.agile-software.site\/?p=1517"},"modified":"2021-08-06T14:44:14","modified_gmt":"2021-08-06T05:44:14","slug":"django-rest-framework%e3%81%9d%e3%81%ae%ef%bc%92","status":"publish","type":"post","link":"https:\/\/agile-software.net\/?p=1517","title":{"rendered":"Django-rest-framework\u305d\u306e\uff12"},"content":{"rendered":"\n<p>Model \u306b\u7d10\u3065\u304f Field \u3092 Serializer \u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3068\u3057\u3066\u81ea\u52d5\u7684\u306b\u5b9a\u7fa9\u3057\u3066\u304f\u308c\u308b<br>model\u3092UserModel<br>fields\u306f\u30ec\u30b9\u30dd\u30f3\u30b9\u3067\u8868\u793a\u3059\u308b\u5185\u5bb9\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002<br>extra_kwargs\u306f\u65e2\u5b58\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u306b\u5c5e\u6027\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002&#8217;write_only&#8217;:True\u3068\u3057\u3066\u3044\u308b\u7406\u7531\u306fSerializer \u304b\u3089\u5024\u3092\u5165\u308c\u305f\u3044\u3051\u3069\u3001\u8aad\u307f\u51fa\u3057\u306f\u3057\u305f\u304f\u306a\u3044\u305f\u3081\u3067\u3059\u3002\u307e\u305f\u3001\u30d1\u30b9\u30ef\u30fc\u30c9\u306e\u5165\u529b\u3092\u5fc5\u9808\u306b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>create_user\u3092validata\u306b\u3059\u308b\u3053\u3068\u3067\u30cf\u30c3\u30b7\u30e5\u5316\u3057\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u4fdd\u5b58\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b<br><code>create()<\/code>,<code>update()<\/code>\u30e1\u30bd\u30c3\u30c9\u306f\u3001<code>serializer.save()<\/code>\u304c\u547c\u3070\u308c\u305f\u6642\u306b\u3069\u306e\u3088\u3046\u306b\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210\u30fb\u5909\u66f4\u3059\u308b\u304b\u3092\u898f\u5b9a\u3059\u308b\u3002<\/p>\n\n\n\n<div class=\"wp-block-jin-gb-block-box-with-headline kaisetsu-box5\"><div class=\"kaisetsu-box5-title\">create()\u30e1\u30bd\u30c3\u30c9<\/div><\/div>\n\n\n\n<div class=\"wp-block-jin-gb-block-icon-box jin-icon-caution jin-iconbox\"><div class=\"jin-iconbox-icons\"><i class=\"jic jin-ifont-caution jin-icons\"><\/i><\/div><div class=\"jin-iconbox-main\">\n<p><code>validated_data<\/code>\u306b\u306f\u3001\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3092\u901a\u904e\u3057\u305f\u30c7\u30fc\u30bf\u304c\u683c\u7d0d\u3055\u308c\u307e\u3059<\/p>\n<\/div><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">ModelSerializer<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u30d1\u30b9\u30ef\u30fc\u30c9\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u30cf\u30c3\u30b7\u30e5\u5316\u3057\u3066\u767b\u9332\u3059\u308b<\/h4>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\" data-file=\"serializers.py\"><code>from rest_framework import serializers\nfrom .models import Task\nfrom django.contrib.auth.models import User\nfrom rest_framework.authtoken.models import Token\n\nclass UserSerializer(serializers.ModelSerializer):\n    class Meta:\n        model = User\n        fields = (&#39;id&#39;,&#39;username&#39;,&#39;passsword&#39;)\n        extra_kwargs = {&#39;password&#39;:{&#39;write_only&#39;:True,&#39;required&#39;:True}}\n\n    def create(self,validated_data):\n        user = User.objects.create_user(**validated_data)\n        Token.object.create(user=user)\n        return user<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Model \u306b\u7d10\u3065\u304f Field \u3092 Serializer \u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3068\u3057\u3066\u81ea\u52d5\u7684\u306b\u5b9a\u7fa9\u3057\u3066\u304f\u308c\u308bmodel\u3092UserModelfields\u306f\u30ec\u30b9\u30dd\u30f3\u30b9\u3067\u8868\u793a\u3059\u308b\u5185\u5bb9\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002extra_kwargs\u306f\u65e2\u5b58\u306e\u30d5\u30a3\u30fc<\/p>\n","protected":false},"author":1,"featured_media":1559,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jin_ogp_image_url":"","_jin_last_featured_id":0,"footnotes":""},"categories":[39],"tags":[],"class_list":["post-1517","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-django-rest-framework"],"_links":{"self":[{"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/posts\/1517","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1517"}],"version-history":[{"count":2,"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/posts\/1517\/revisions"}],"predecessor-version":[{"id":1521,"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/posts\/1517\/revisions\/1521"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/media\/1559"}],"wp:attachment":[{"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}