在内容空间中请求所有条目,但未按字段名称查找条目

时间:2019-04-20 20:49:55

标签: python-3.x contentful contentful-api

我有一个内容模型: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'>

我想知道为什么由于条目具有两个属性categoryNamecategoryName而无法识别属性categoryBlob

我遵循的documentation以相同的方式进行操作:

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

我想念什么?

1 个答案:

答案 0 :(得分:0)

显然,getattr不允许使用驼峰形参数。

我更改了:

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

收件人:

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

即使该属性的名称为categoryName,它仍然有效。

相关问题