2014-09-04 124 views
2

我正在构建一个Django应用程序,通过LinkedIn API下载一些数据并将其存储到MongoDB。我正在使用python-linkedin库。一个函数获取组帖子并将这些帖子作为字典对象返回。Python,MongoDB,Mongoengine - TypeError:字符串索引必须是整数,而不是str

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

====================================================================== 
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的OR的限制M + MongoDB,还是我做了其他的错误?

+1

哪里是'api.get_or_create_post代码(A1) '?请检查'a1'的值,它必须是'dict'。 – Nilesh 2014-09-04 03:38:23

+1

我刚刚添加了tests.py以进一步说明我如何获取错误。 – 2014-09-04 03:46:52

+0

用回答:) – Nilesh 2014-09-04 03:51:18

回答

2

问题是

a1 = ["values"][0] 

检查输出

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

您可能需要编写像

a1 = a["values"][0] 
+0

啊 - 谢谢你!看起来这是另一种疲倦,疲惫的眼睛:) – 2014-09-04 03:54:28

相关问题