gpt4 book ai didi

Python、MongoDB、Mongoengine - TypeError : string indices must be integers, 不是 str

转载 作者:可可西里 更新时间:2023-11-01 10:30:50 27 4
gpt4 key购买 nike

我正在构建一个 Django 应用程序,它通过 LinkedIn API 下载一些数据并将其存储到 MongoDB。我正在使用 python-linkedin图书馆。一个函数获取群组帖子并将这些帖子作为字典对象返回。

在测试时我收到以下类型错误:

 ======================================================================
ERROR: test_get_or_create_post (providers.linkedin.tests.LinkedInTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/josephfusaro/explorro/explorro/app/providers/linkedin/tests.py", line 36, in test_get_or_create_post
api.get_or_create_post(a1)
File "/Users/josephfusaro/explorro/explorro/app/providers/linkedin/api.py", line 134, in get_or_create_post
existing_post = Post.objects.get(post_id=post['id'])

TypeError: string indices must be integers, not str

这是返回的字典对象的示例,表示 LinkedIn 组帖子和创建帖子的用户。

{u'creator': 
{u'firstName': u'Joe',
u'headline': u'General Manager',
u'id': u'Wr4g5xEN4I',
u'lastName': u'P.',
u'pictureUrl': u'http://m.c.lnkd.licdn.com/mpr/mprx/0_x...xJkwu'},
u'id': u'g-60415-S-5913079393848621697',
u'title': u'How do you use LinkedIn groups for business?',
u'type': {u'code': u'standard'}
}

我正在尝试将这些对象存储在 MongoDB 中。这是(大部分)documents.py 文件:

 # documents.py

from mongoengine import (
Document,
ReferenceField,
StringField,
IntField,
DateTimeField,
URLField,
DictField,
ListField, DoesNotExist, NotUniqueError)
from abc import ABCMeta, abstractmethod


class LinkedInUser(Document):
linkedin_user_id = StringField(required=True)
screen_name = StringField()
first_name = StringField()
last_name = StringField()
description = StringField()
url = URLField()
profile_image_url = URLField()


class Post(Document):
linkedin_user = ReferenceField(LinkedInUser,required=True)
post_id = StringField(required=True)
title = StringField()

这是接收帖子(预期为字典)并将其保存在 MongoDB 中的函数

 # api.py

def get_or_create_post(post):
try:
existing_post = Post.objects.get(post_id=post['id'])
return existing_post
except DoesNotExist:
try:
new_post = Post()
new_post.linkedin_user = get_or_create_linkedin_user(
post['creator']
)
new_post.post_id = post['id']
new_post.title = post['title']
new_post.save()

return new_post
except NotUniqueError:
return get_or_create_post(post)

请注意,我在运行 Django 测试时遇到错误

 # tests.py

from django.test import TestCase
from .models import OauthToken
from accounts.models import EmailUser
from . import api


class LinkedInTestCase(TestCase):
'''To run these tests: python app/manage.py test linkedin'''

def setUp(self):
cust_user = EmailUser.objects.create()
oauth = OauthToken.objects.create(user=cust_user)
oauth.access_token = "AQUaXm...klsdfQ"
oauth.save()

def test_get_or_create_post(self):
oauth = OauthToken.objects.get(user=0)
auth = api.get_linkedin_api(access_token=oauth.access_token)
posts = api.get_linkedin_group_digest(auth) # create generator object and set to 'posts'
a = next(posts) # get item from generator object above
a1 = ["values"][0] # Get first post from dict
api.get_or_create_post(a1)

这是 Django ORM + MongoDB 的限制,还是我做错了什么?

最佳答案

问题是

a1 = ["values"][0]

检查输出

>>> a1 = ["values"][0]
>>> print a1
values

你可能不得不这样写

a1 = a["values"][0]

关于Python、MongoDB、Mongoengine - TypeError : string indices must be integers, 不是 str,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25656884/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com