AttributeError: 'NoneType' object has no attribute 'serialize' with PynamoDB

48 Views Asked by At

I have a DynamoDB table that stores jobs opening details.

Below is how I configured my DynamoDB table:

enter image description here

enter image description here

I am now trying to retrieve all items using the PynamoDB library from one of my index recruiter_index using the below code:

opening.py

from pynamodb.models import Model
from pynamodb.attributes import UnicodeAttribute
from kink import di, inject
from models.recruiter_index import RecruiterIndex


@inject
class Opening(Model):
"""
A DynamoDB Opening Table
"""

class Meta:
    table_name = "mo-bizin-travay"
    region = "eu-west-1"

recruiter_index = RecruiterIndex()
id = UnicodeAttribute(hash_key=True)
title = UnicodeAttribute()
posted_date = UnicodeAttribute()
recruiter = UnicodeAttribute()
updated_at = UnicodeAttribute()

recruiter_index.py

from pynamodb.indexes import GlobalSecondaryIndex, AllProjection
from pynamodb.attributes import UnicodeAttribute


class RecruiterIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index
"""

class Meta:
    # index_name is optional, but can be provided to override the default name
    index_name = "recruiter_index"
    read_capacity_units = 2
    write_capacity_units = 1
    # All attributes are projected
    projection = AllProjection()

# This attribute is the hash key for the index
# Note that this attribute must also exist
# in the model
recruiter = UnicodeAttribute()

main.py

def main(event, context):
for item in Opening.recruiter_index.query("MCB"):
    print("Item queried from index: {0}".format(item))

But i am getting the below error:

Traceback (most recent call last):
  File "/Users/mervin.hemaraju/Projects/Personal/mo-bizin-travay/runner.py", line 8, in <module>
    main(EVENT, CONTEXT)
  File "/Users/mervin.hemaraju/Projects/Personal/mo-bizin-travay/di.py", line 32, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mervin.hemaraju/Projects/Personal/mo-bizin-travay/main.py", line 12, in main
    for item in Opening.recruiter_index.query("MCB"):
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mervin.hemaraju/Projects/Personal/mo-bizin-travay/.venv/lib/python3.11/site-packages/pynamodb/indexes.py", line 89, in query
    return self._model.query(
           ^^^^^^^^^^^^^^^^^^
  File "/Users/mervin.hemaraju/Projects/Personal/mo-bizin-travay/.venv/lib/python3.11/site-packages/pynamodb/models.py", line 661, in query
    hash_key = cls._indexes[index_name]._hash_key_attribute().serialize(hash_key)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'serialize'
0

There are 0 best solutions below