Ojonugwa Jude Ochalifu
Ojonugwa Jude Ochalifu

Reputation: 27257

Request for all entries in a Contentful space not finding entries by field name

I have a content model: Category, with two entries for the Category content.

which I am retrieving with this function:

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)

The output for this is:

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

I'm wondering why the attribute categoryName not being recognized since the entry has two attributes categoryName and categoryBlob

The documentation I'm following does it in the same way:

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

What am I missing?

Upvotes: 0

Views: 272

Answers (1)

Ojonugwa Jude Ochalifu
Ojonugwa Jude Ochalifu

Reputation: 27257

Apparently, getattr doesn't allow camelcase arguments.

I changed:

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

to:

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

even though the name of the attribute is categoryName and it worked.

Upvotes: 0

Related Questions