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

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

如何獲取字典并發(fā)送JSON響應(yīng)

如何獲取字典并發(fā)送JSON響應(yīng)

湖上湖 2021-03-29 13:09:09
我有以下功能,def facebooktest(request):    fb_value = ast.literal_eval(request.body)    fb_foodies = Foodie.objects.filter(facebook_id__in = fb_value.values())    for fb_foodie in fb_foodies:        state = request.user.relationships.following().filter(username = fb_foodie.user.username).exists()        userData = {            'fbid': fb_foodie.facebook_id,            'followState': int(state),                }基本上我正在檢查我的django應(yīng)用程序上有哪些用戶的Facebook朋友。如果是,則返回followState。followState基本上返回1或0。如果用戶已經(jīng)在我的Django應(yīng)用程序上關(guān)注他們,則返回1;如果用戶在我的Django應(yīng)用程序上未關(guān)注其Facebook朋友,則返回0。我想將json類型字典返回給該用戶,如下所示:[{fbid:222222222222, followState: 0}, {fbid:111111111111, followState: 1}, {fbid:435433434534, followState:1}]
查看完整描述

3 回答

?
LEATH

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

def facebooktest(request):

    fb_value = ast.literal_eval(request.body)

    fb_foodies = Foodie.objects.filter(facebook_id__in = fb_value.values())

    response = []

    for fb_foodie in fb_foodies:

        state = request.user.relationships.following().filter(username = fb_foodie.user.username).exists()

        userData = {

            'fbid': fb_foodie.facebook_id,

            'followState': int(state),

                }

        response.append(userData)

    return json.dumps(response)


查看完整回答
反對 回復(fù) 2021-04-02
?
胡子哥哥

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

我認(rèn)為您正在尋找這個:

return HttpResponse(simplejson.dumps(response_dict), mimetype='application/json')

其中“ response_dict”將是您的字典。


查看完整回答
反對 回復(fù) 2021-04-02
?
楊__羊羊

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

django.forms.models包中有一個用于該功能的模型:model_to_dict


from django.forms.models import model_to_dict


model_to_dict(your_model, fields=[], exclude=[])

從幫助中:


model_to_dict(instance, fields=None, exclude=None)

    Returns a dict containing the data in ``instance`` suitable for passing as

    a Form's ``initial`` keyword argument.


    ``fields`` is an optional list of field names. If provided, only the named

    fields will be included in the returned dict.


    ``exclude`` is an optional list of field names. If provided, the named

    fields will be excluded from the returned dict, even if they are listed in

    the ``fields`` argument.


查看完整回答
反對 回復(fù) 2021-04-02
  • 3 回答
  • 0 關(guān)注
  • 191 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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