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

Getting KeyError when using .exclude in a QuerySetProxy #1087

Open
satinfive opened this issue May 21, 2023 Discussed in #1086 · 0 comments
Open

Getting KeyError when using .exclude in a QuerySetProxy #1087

satinfive opened this issue May 21, 2023 Discussed in #1086 · 0 comments

Comments

@satinfive
Copy link

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!

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

1 participant