[Python] pynamodb - Get DB entries with only hash key

Discussão em 'Python' iniciado por Stack, Fevereiro 16, 2025.

  1. Stack

    Stack Membro Participativo

    Using pynamodb, I want to get all range keys that match a certain hash key in a table.

    I know I can do a scan and then filter out entries that match the hash key like so:

    from pynamodb.models import Model
    from pynamodb.attributes import UnicodeAttribute

    class Users(Model):
    class Meta:
    table_name = 'user_posts'
    username = UnicodeAttribute(hash_key=True)
    post_id = UnicodeAttribute(range_key=True)

    # Get all post_id's for a username
    user = 'johndoe22'
    posts = []

    for entry in Users.scan():
    if entry.username == user:
    posts.append(entry.post_id)


    I'd like to move the filtering logic to the query level so I don't have to pull down the entire contents of the db. How can I achieve this?

    Continue reading...

Compartilhe esta Página