LinkedIn API를 통해 일부 데이터를 다운로드하고 MongoDB에 저장하는 Django 앱을 만들고 있습니다. 나는 python-linkedin 라이브러리를 사용하고 있습니다. 하나의 함수는 그룹 포스트를 가져와 dict 오브젝트로 리턴합니다.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
는 링크드 인 그룹 포스트와 포스트를 생성 한 사용자를 나타내는 반환 된 딕셔너리 객체의 예입니다.
{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()
의 (a 딕셔너리로 예상) 게시물을 소요하고
# 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)
참고 MongoDB를 저장하는 기능입니다 I 장고를 실행하는
# 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)
을 테스트하는 동안 오류를 얻고 것은이 장고 OR의 제한 사항입니다 M + MongoDB, 아니면 다른 일을 잘못하고 있습니까? api.get_or_create_post`에 대한 코드는 어디
처럼 작성해야 할 수도 있습니다
출력입니다 (A1) '? 'a1' 값을 확인하십시오.'dict'이어야합니다. – Nilesh
오류가 어떻게 발생하는지에 대한 자세한 설명을 위해 tests.py를 추가했습니다. –
답글로 응답 : – Nilesh