Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'NoneType' object has no attribute 'serialize' #1232

Open
mervinhemaraju opened this issue Mar 8, 2024 · 3 comments
Open

Comments

@mervinhemaraju
Copy link

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'
@vanlenhat
Copy link

you can try

Opening.query(recruiter_index == "MCB")

@mervinhemaraju
Copy link
Author

Opening.query(recruiter_index == "MCB")

This doesn't work. I get the error

Traceback (most recent call last):
  File "/Users/mervin.hemaraju/Projects/Personal/mo-bizin-travay/iac/scripts/scraper/runner.py", line 9, in <module>
    main(EVENT, CONTEXT)
  File "/Users/mervin.hemaraju/Projects/Personal/mo-bizin-travay/iac/scripts/scraper/models/core/di.py", line 35, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mervin.hemaraju/Projects/Personal/mo-bizin-travay/iac/scripts/scraper/test.py", line 17, in main
    for item in Opening.query(recruiter_index == "MCB"):
                              ^^^^^^^^^^^^^^^
NameError: name 'recruiter_index' is not defined

@mervinhemaraju
Copy link
Author

Any updates on this please ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants