今、Djangoを練習していてDjangoとReactを使ってサイトを作っているのですが下記のようなコードで実行するとprojectの名前を表示したいのに番号が出てきてしまいます。(下記画像)
Djangoのadminではreturn self.project.name
と設定しているのでちゃんと日本語でプロジェクト名が表示されるのですが、Reactだとprojectだと数字、project.nameだと何も表示されなくなってしまいます。
大変わかりにくい説明で申し訳ないのですが、詳しい方教えていただけると幸いです。
Backend(Django)
models.py
Django
1from django.db import models 2from django.utils import timezone 3from django.conf import settings 4 5class Category(models.Model): 6 COLOR_CHOICE =( 7 ('#039BE5','SkyBlue'), 8 ('#4766ea','Blue'), 9 ('#F6BF26','Yellow'), 10 ('#2da608','Green'), 11 ('#D50000','Red'), 12 ('#FF9800','orange'), 13 ('#d84182','Pink'), 14 ('#bf7000','Brown'), 15 ('#7427e8','Purple'), 16 ('#616161','Gray'), 17 ('#000000','Black'), 18 ) 19 name = models.CharField(max_length=255) 20 author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) 21 color = models.CharField(choices=COLOR_CHOICE, max_length=100) 22 created_at = models.DateTimeField(auto_now_add=True) 23 24 def __str__(self): 25 return self.name 26 27class Project(models.Model): 28 category = models.ForeignKey(Category, on_delete=models.PROTECT) 29 name = models.CharField(max_length=255) 30 timestamp = models.DateTimeField(auto_now_add=True) 31 32 def __str__(self): 33 return self.name 34 35class Record(models.Model): 36 project = models.ForeignKey(Project, on_delete=models.PROTECT) 37 time = models.DurationField() 38 timestamp = models.DateTimeField(auto_now_add=True) 39 40 def __str__(self): 41 return self.project.name 42
views.py
Django
1from django.shortcuts import render 2from rest_framework import generics 3from .models import Record 4from .serializers import RecordSerializer 5 6 7class ListRecord(generics.ListAPIView): 8 queryset = Record.objects.all() 9 serializer_class = RecordSerializer 10 11 12class DetailRecord(generics.RetrieveAPIView): 13 queryset = Record.objects.all() 14 serializer_class = RecordSerializer
serializers.py
Django
1from rest_framework import serializers 2from .models import Category, Project, Record 3 4class CategorySerializer(serializers.ModelSerializer): 5 class Meta: 6 model = Category 7 fields = ('id','name','author','color','created_at',) 8 9class ProjectSerializer(serializers.ModelSerializer): 10 class Meta: 11 model = Project 12 fields = ('id','category','name','timestamp',) 13 14class RecordSerializer(serializers.ModelSerializer): 15 class Meta: 16 model = Record 17 fields = ('id','project','time','timestamp',)
urls.py
Django
1from django.urls import path 2 3from .views import ListRecord, DetailRecord 4 5urlpatterns = [ 6 path('<int:pk>/', DetailRecord.as_view()), 7 path('', ListRecord.as_view()), 8]
##Frontend(React)
App.js
React
1import React, { Component } from 'react'; 2import axios from 'axios'; 3 4class App extends Component { 5 state = { 6 record: [] 7 }; 8 9 componentDidMount() { 10 this.getRecord(); 11 } 12 13 getRecord() { 14 axios 15 .get('http://127.0.0.1:8000/api/') 16 .then(res => { 17 this.setState({ record: res.data }); 18 }) 19 .catch(err => { 20 console.log(err); 21 }); 22} 23 render() { 24 return ( 25 <div> 26 {this.state.record.map(item => ( 27 <div key={item.id}> 28 <h1>{item.project}</h1> 29 <p>{item.time}</p> 30 </div> 31 ))} 32 </div> 33 ); 34 } 35} 36 37 38export default App;
あなたの回答
tips
プレビュー