You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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:
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.
The text was updated successfully, but these errors were encountered: