Skip to content

Getting KeyError when using .exclude in a QuerySetProxy #1087

@satinfive

Description

@satinfive

Discussed in #1086

Originally posted by satinfive May 21, 2023
Hi!

I am not sure if I am doing things right, that's why I wanted to ask before opening this as an issue.

I have defined two models: Battle and User:

class User(ormar.Model):

    class Meta(base.BaseMeta):
        tablename = "users"
        queryset_class = UserQuerySet

    id: str = ormar.String(primary_key=True, max_length=50)
    username: str = ormar.String(max_length=255)
    team_id: str = ormar.String(max_length=50)
class Battle(ormar.Model):
    class Meta(base.BaseMeta):
        tablename = "battles"

    id: int = ormar.Integer(primary_key=True, autoincrement=True)
    player1: models.User = ormar.ForeignKey(models.User, related_name="player1")
    player2: models.User = ormar.ForeignKey(models.User, related_name="player2")
    current_status: BattleStatus = ormar.String(max_length=100, default=BattleStatus.ONGOING.value, choices=list(BattleStatus), )
    winner: Optional[models.User] = ormar.ForeignKey(models.User, nullable=True, related_name="winner")

Given those, I have user_a as an instance of User. I want to fetch all users in database that are not in a battle with given user_a.
I tried to execute the following query:

            users_available_for_battle = await User.objects.available_in_workspace(
                team_id=team_id).exclude(
                id=user_a_id).select_related(
                ['player1', 'player2']).exclude(
                player1__id=user_a_id).exclude(
                player2_iid=user_a_id)

But I am getting all the time KeyError: player1 .

It's possible that I am not doing the query correctly, so I'd like some feedback to know if it's that the case or there is something that is not working properly in the library.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions