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

ModelChoiceFilter / ForeignKey lookup exact causing issues #1466

Open
pcorsaro opened this issue Jan 3, 2022 · 0 comments
Open

ModelChoiceFilter / ForeignKey lookup exact causing issues #1466

pcorsaro opened this issue Jan 3, 2022 · 0 comments

Comments

@pcorsaro
Copy link

pcorsaro commented Jan 3, 2022

This is probably a weird edge case, but I ran into an issue today using the ModelChoiceFilter on a foreign key field. The model that the foreign key was pointing to has a field named exact in it that was screwing up all of the filtering. Here are the two models and the FilterSet class to look at:

class Review(models.Model):
    game = models.ForeignKey(Game, on_delete=models.CASCADE)

class Game(models.Model):
    exact = models.BooleanField(default=True)
    name = models.CharField(max_length=300, db_index=True)

class ReviewFilter(filters.FilterSet):
    game = filters.ModelChoiceFilter(queryset=Game.objects.all())

    class Meta:
        model = Review

Because django-filter by default builds an __exact lookup, I kept getting this weird error that the game value had to be true or false. Django was trying to do a query on the exact field in my Game model instead of trying to do an __exact lookup, which makes sense. It's a super edge case, and I was able to get around it by just creating my own filter method to call on the game filter and do a queryset.filter(game=value) call instead of queryset.filter(game__exact=value) that django-filter does. It doesn't seem like the __exact is really needed anyway on a ModelChoiceFilter field, so maybe it could be pulled out in the future. Just thought I'd throw this out there for you to see. It could almost be called a bug in Django that they let you use exact as a field name.

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