Python, MongoDB, Mongoengine - TypeError: string indices must be integers, not str

Joe Fusaro picture Joe Fusaro · Sep 4, 2014 · Viewed 9.4k times · Source

I am building a Django app that downloads some data via the LinkedIn api and stores it to MongoDB. I am using the python-linkedin library. One function gets group posts and returns these posts as dict objects.

While testing I am getting the following TypeError:

 ======================================================================
 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

Here is an example of a dict object that is returned, representing a LinkedIn group post and the user that created the post.

{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'}
}

I am attempting to store these objects in MongoDB. Here is the (most of) the documents.py file:

 # 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()

Here is the function that takes a post (expected as a dict) and saves it 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)

Note that I am getting the error while running Django tests

 # 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)

Is this a limitation of the Django ORM + MongoDB, or am I doing something else wrong?

Answer

Nilesh picture Nilesh · Sep 4, 2014

Problem is

a1 = ["values"][0]

check the output

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

You might be have to write like

a1 = a["values"][0]