0

我有一个内容模型:Category,有两个类别内容条目。

我正在使用此功能检索:

def categories(request):
    contentful_client = contentful.Client(CONTENTFUL_SPACE_ID,
                                      CONTENTFUL_TOKEN_ID)

    category_entries = contentful_client.entries()

    for entry in category_entries:
        print(getattr(entry, 'categoryName', 'not found'))

    for entry in category_entries:
        print(entry)

    count = len(category_entries)
    return HttpResponse('There are %d categories' % count)

输出是:

 not found
 not found
 <Entry[category] id='7hzRlNZw9cvKeYBQPGRV9a'>
 <Entry[category] id='2ZV3W30qLSosL5PPmKEoD5'>

我想知道为什么该属性categoryName没有被识别,因为该条目有两个属性categoryNamecategoryBlob

我正在关注的文档以相同的方式执行此操作:

for entry in entries:
    print(getattr(entry, 'product_name', 'Not a product'))


Not a product
Not a product
Whisk Beater
Hudson Wall Cup
Not a product
Not a product
Not a product
Not a product
SoSo Wall Clock
Not a product
Not a product
Not a product
Playsam Streamliner Classic Car, Espresso

我错过了什么?

4

1 回答 1

0

显然,getattr不允许驼峰式参数。

我变了:

print(getattr(entry, 'categoryName', 'not found'))

至:

print(getattr(entry, 'category_name', 'not found'))

即使属性的名称是categoryName并且它有效。

于 2019-04-21T20:56:42.147 回答