为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

HUX布斯 2022-06-14 17:53:06
我正在学习 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>    )}
查看完整描述

2 回答

?
慕雪6442864

TA贡献1812条经验 获得超5个赞

您可以连接全名,将其注释到查询集上,然后将其添加到序列化程序中:


from django.db.models import F, Value


class ArticlesViewset(viewsets.ModelViewSet):

    permission_classes = [permissions.AllowAny]

    queryset = Articles.objects.all()

    serializer_class = ArticleSerializer


    def get_queryset(self):

        return self.queryset.annotate(

            authors_name=Concat('author__first_name', Value(" "), 'author__last_name')

        )


class ArticleSerializer(serializers.ModelSerializer):

    authors_name = serializers.CharField(required=False)

    class Meta:

        model = Articles

        fields = ('article_title', 'description', 'created_at', 'authors_name')


查看完整回答
反对 回复 2022-06-14
?
慕姐4208626

TA贡献1852条经验 获得超7个赞

我在序列化程序类中进行了以下更改,并且能够获取作者姓名


#Article serializer

class ArticleSerializer(serializers.ModelSerializer):

  author = serializers.CharField(required=False)

  class Meta:

    model = Articles

    fields = ('article_title', 'description','created_at', 'author')


查看完整回答
反对 回复 2022-06-14
  • 2 回答
  • 0 关注
  • 107 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信