在前端(React 组件)中显示值,这是 django 模型中的外键

我正在学习 django rest 框架。我想在 JSON 中获取作者全名而不是 id,下面是GET request,有没有办法?Articles从管理站点创建。我试图在前端获得价值article.author


[

    {

        "id": 1,

        "article_title": "Premier League",

        "description": "The Premier League is the top level of the English football league system. Contested by 20 clubs, it operates on a system of promotion and relegation with the English Football League. The Premier League is a corporation in which the member clubs act as shareholders.",

        "created_at": "2019-10-02T08:59:24.883450Z",

        "author": 1

    },

    {

        "id": 2,

        "article_title": "Cricket in England",

        "description": "Cricket is one of the most popular sports in England, and has been played since the 16th century. Marylebone Cricket Club, based at Lord's, developed the modern rules of play and conduct.",

        "created_at": "2019-10-02T08:59:57.075912Z",

        "author": 2

    },


]

反应组件的渲染功能。article.author_id给出空白并将article.author_id作者显示id为integer. 我应该返回什么来显示作者全名?


render() {

    return (

        <Fragment>

            <h2>Articles</h2>

            <div >

                {

                    this.props.articles.map(article => (

                        <div className="card" key={article.id}>

                            <div className="card-body">

                                <div className="card-title"> <b>{article.article_title}</b></div>

                                <div className="card-text"> {article.description}</div>

                                <div> Author: {article.author_id}</div>

                                <div className="card-footer text-muted"> Created date: {article.created_at}</div>

                            </div>

                            <br></br>

                        </div>


                    ))


                }

            </div>

        </Fragment>

    )

}


HUX布斯
浏览 108回答 2
2回答

慕雪6442864

您可以连接全名,将其注释到查询集上,然后将其添加到序列化程序中:from django.db.models import F, Valueclass ArticlesViewset(viewsets.ModelViewSet):&nbsp; &nbsp; permission_classes = [permissions.AllowAny]&nbsp; &nbsp; queryset = Articles.objects.all()&nbsp; &nbsp; serializer_class = ArticleSerializer&nbsp; &nbsp; def get_queryset(self):&nbsp; &nbsp; &nbsp; &nbsp; return self.queryset.annotate(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; authors_name=Concat('author__first_name', Value(" "), 'author__last_name')&nbsp; &nbsp; &nbsp; &nbsp; )class ArticleSerializer(serializers.ModelSerializer):&nbsp; &nbsp; authors_name = serializers.CharField(required=False)&nbsp; &nbsp; class Meta:&nbsp; &nbsp; &nbsp; &nbsp; model = Articles&nbsp; &nbsp; &nbsp; &nbsp; fields = ('article_title', 'description', 'created_at', 'authors_name')

慕姐4208626

我在序列化程序类中进行了以下更改,并且能够获取作者姓名#Article serializerclass ArticleSerializer(serializers.ModelSerializer):&nbsp; author = serializers.CharField(required=False)&nbsp; class Meta:&nbsp; &nbsp; model = Articles&nbsp; &nbsp; fields = ('article_title', 'description','created_at', 'author')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python