下記のモデルBook, Author, Publisherから、
DjangoRestFrameWorkのSerializerを用いてJsonを出力したのですが、期待した結果になりません。
どのようにすれば期待した結果を得られるでしょうか
python
1# 出力された結果 2{ 3 "title" : "タイトル", 4 "publication_date" : "2021-05-01", 5 "authors": [1, 2], 6 "publisher": 1, 7}
python
1# 期待された結果 2{ 3 'pk': 0, 4 "title" : "タイトル", 5 "publication_date" : "2021-05-01", 6 "authors": [ 7 { 8 "pk": 0, 9 "author_name": "著者1, 10 }, 11 { 12 "pk": 1, 13 "author_name": "著者2, 14 }, 15 ], 16 "publisher": { 17 "pk": 0, 18 "publisher_name": "出版社1", 19 }, 20}
python
1class Publisher(models.Model): 2 publisher_name = models.CharField(max_length=30) 3 4class Author(models.Model): 5 author_name = models.CharField(max_length=40) 6 7class Book(models.Model): 8 title = models.CharField(max_length=100) 9 authors = models.ManyToManyField(Author) 10 publisher = models.ForeignKey(Publisher) 11 publication_date = models.DateField()
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。