第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在前端(React 組件)中顯示值,這是 django 模型中的外鍵

在前端(React 組件)中顯示值,這是 django 模型中的外鍵

HUX布斯 2022-06-14 17:53:06
我正在學(xué)習(xí) django rest 框架。我想在 JSON 中獲取作者全名而不是 id,下面是GET request,有沒有辦法?Articles從管理站點創(chuàng)建。我試圖在前端獲得價值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    },]反應(yīng)組件的渲染功能。article.author_id給出空白并將article.author_id作者顯示id為integer. 我應(yīng)該返回什么來顯示作者全名?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貢獻(xiàn)1812條經(jīng)驗 獲得超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')


查看完整回答
反對 回復(fù) 2022-06-14
?
慕姐4208626

TA貢獻(xiàn)1852條經(jīng)驗 獲得超7個贊

我在序列化程序類中進行了以下更改,并且能夠獲取作者姓名


#Article serializer

class ArticleSerializer(serializers.ModelSerializer):

  author = serializers.CharField(required=False)

  class Meta:

    model = Articles

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


查看完整回答
反對 回復(fù) 2022-06-14
  • 2 回答
  • 0 關(guān)注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號