0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[DRF]結合テーブルの更新(part2)【create】【drf-writable-nested】

Last updated at Posted at 2021-04-14

Django-rest-frameworkでAPIを作成します

前回までの記事一覧です。

基本編
[DRF]apiの作成(patch)
[DRF]apiの作成(post)
[DRF]apiの作成(get)part2
[DRF]apiの作成(get)part1
応用編
[DRF]結合テーブルの更新(part1)
[DRF]結合テーブルの取得

前回の記事では、serializerのcreate()メソッドをオーバーライドしてレコードを新規作成しました。
今回はdrf-writable-nestedを使用してみます。

model

modelは以下のとおりです。

models.py
from django.db import models

DISTRICT_CATEGORIES = [
    ("1", "地区1"),
    ("2", "地区2"),
    ("3", "地区3"),
    ("4", "地区4"),
]


class Student(models.Model):
    """生徒情報"""

    # 生徒ID
    student_id = models.CharField(max_length=4, primary_key=True)

    # クラス
    class_no = models.CharField(max_length=1)

    # 出席番号
    attendance_no = models.IntegerField()

    # 名前
    name = models.CharField(max_length=20)

    # 地区番号
    district_no = models.CharField(max_length=1, choices=DISTRICT_CATEGORIES)

    # フリーコメント
    comment = models.CharField(max_length=200,blank=True)

    class Meta:
        constraints = [
            models.UniqueConstraint(
                fields=["class_no", "attendance_no"],
                name="class_attendance_unique"
            ),
        ]


class Exam(models.Model):
    """試験情報"""

    # 国語
    japanese_score = models.IntegerField(null=True, blank=True)

    # 数学
    math_score = models.IntegerField(null=True, blank=True)

    # 英語
    english_score = models.IntegerField(null=True, blank=True)

    # 生徒ID
    student = models.OneToOneField(Student, on_delete=models.CASCADE)

StudentとExamが1対1対応しています。

create

とりあえず新規作成とします。

view

前回と同じです。

views.py
from rest_framework.views import APIView
from rest_framework.response import Response

from ..serializers import StudentSerializer


class StudentExamCreateAPIView(APIView):
    """生徒情報、試験情報を登録"""

    def post(self, request, *args, **kwargs):

        # serializer作成
        serializer = StudentSerializer(data=request.data)

        # validate
        try:
            serializer.is_valid(raise_exception=True)
        except Exception:
            raise Exception

        serializer.save()

        # Response
        return Response({'result':True})

serializer

drf-writable-nestedを使用するので、.create()は必要なく、親のクラスでWritableNestedModelSerializerを継承すれば良いです。

serializer
from rest_framework import serializers
from ..models import Student, Exam
from drf_writable_nested.serializers import WritableNestedModelSerializer



class ExamSerializer(serializers.ModelSerializer):
    """Examクラスのシリアライザ"""

    class Meta:
        model = Exam
        fields=[
            'japanese_score',
            'math_score',
            'english_score'
        ]


class StudentSerializer(WritableNestedModelSerializer):
    """Studentクラスのシリアライザ"""

    exam = ExamSerializer()

    class Meta:
        model = Student
        fields = [
            'student_id',
            'class_no',
            'attendance_no',
            'name',
            'district_no',
            'comment',
            'exam'
        ]

実行

出来ちゃった・・・!

import requests
import json

student_id='0012'
class_no='6'
attendance_no=16
name="更木剣八"
district_no='4'
comment='こめんと'

japanese_score = 20
math_score = 40
english_score = 80
exam = {
    'japanese_score': japanese_score,
    'math_score': math_score,
    'english_score': english_score,
}
body = {
    'student_id':student_id,
    'class_no':class_no,
    'attendance_no':attendance_no,
    'name':name,
    'district_no':district_no,
    'comment':comment,
    'exam': exam,
}

headers = {"Content-Type" : "application/json;charset=UTF-8"}
response = requests.post('http://127.0.0.1:8000/student/student_exam_create/', data=json.dumps(body), headers=headers)

print(response.text)

{"result":true}

新規作成については特に気にせずできるみたいですね。
更新が気になるので、次回以降、記事にしていきます。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?